The Artima Developer Community
Sponsored Link

Java Buzz Forum
Date is depreciated - Calander is King

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ben Hosking

Posts: 208
Nickname: hoskinator
Registered: Apr, 2006

Ben Hosking is Java Developer with about 5 years experience and interest in OO
Date is depreciated - Calander is King Posted: Dec 1, 2006 12:05 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ben Hosking.
Original Post: Date is depreciated - Calander is King
Feed Title: A Funny Java Flavoured Look at the World
Feed URL: http://jroller.com/hoskinator/feed/entries/rss
Feed Description: The blog looks at using Java and programming in general as it pops up in life as a Java programmer. It will have links to interesting Java articles and resources. It will also have a lot of SCJP Java 1.5 information and links as I am currently studying
Latest Java Buzz Posts
Latest Java Buzz Posts by Ben Hosking
Latest Posts From A Funny Java Flavoured Look at the World

Advertisement

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

Read: Date is depreciated - Calander is King

Topic: The 2 GPLv3 Compaitiabile Licenses right out of box Previous Topic   Next Topic Topic: PeopleOverProcess.com: RedMonk Radio Episode 35 - Welcome, Anne Zelenka!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use