A Funny Java Flavoured Look at the World

Wednesday, November 29, 2006

Date is depreciated - Calander is King

This week I had been working with dates which is sure sign of a lot of tricky nitpicking code and lots of little methods each testing certain bits of the code.

The first thing that got me annoyed was the Date class is basically all depreciated except for just creating a date. This is fine and the reason for this is (I think) because people needed a locale specific date or something like that. This is where I started to gnash my teeth a bit because it didn't really point me in the direction I should be going. I think it's the Calendar class and from studying for my SCJP 5 exam. I found the help for the Calendar class not that helpful, how do you set the flipping thing. Here is a link if you want to peruse yourself

Firstly the Calendar class is abstract so to get an instance of it you have to do this

Calendar rightNow = Calendar.getInstance();

or choose a different method to get the right flavour for you. Then to change the date and time from right now you have to set the date, it seem obvious to me now but I was looking through the documentation rather puzzled for a good five minutes before I just went into the code and used my IDE to get the methods available the rightNow variable above.

rightNow.setTime(Date)


I think this is what threw me off a bit because it was setTime and I was thinking about setting a date. Once you have got you Calendar instance set up with the correct time, the Calendar class is very good with the lots of methods to get Time or Months, first day of the week, number of days in month, all very nice.

Earlier on in the day though I was wanting to compare some dates and found this code example to do it, although this uses the Date object. I actually didn't really want to compare the actual date, all I wanted to do was see if the date was in a different month, which I did by getting the month from the calendar instance and then comparing the months.

This was what I was going to blog about all along, how to parse a date from a String value, this example was great, straight to the point

String s = "15-05-2005 5:55:55";

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = df.parse(s);

Above is the basic code to parse the date, I don't think I have used the SimpleDateFormat tool before but it made it quite simple, although I did have a bit of problem because I put small mm instead of big MM when I tried to change the format of the date a bit.

So there you go there were my adventures with dates, the best thing with working with dates is that you can test your functions easily with unit tests because generating some data for it is easy. This means that although my code was doing a lot of small tests at least I could easily and quickly test the code and be confident that it was doing what it should.



If you like this blog or and fancy something a bit less technical with some laughing thrown in then check out my other blog Amusing IT Stories. Which is a blog about funny and amusing stories from the IT environment and the office. It is a mix of news, office humour, IT stories, links, cartoons and anything that I find funny

4 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home