• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date Calculation in JAVA - Is it really trivial ?  RSS feed

 
Ahsan Hadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some guidance in doing this java assignment, with some procedural programming languages this would be a piece of cake.
Basically what i have so far is that I have extracted a string from a file and converted it into a date using DateFormat or so I think. I want to do some calculations with this date object, e.g subtract this date from current date and multiply by 24 to give me a float object which i can use for further calculations.
Many Thanks in Anticipation.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the API docs for GregorianCalendar, including the methods it inherits from Calendar. There are methods there that will allow you to do math on dates. SimpleDateFormat (extends DateFormat) will also be useful as it allows you to get/set a Calendar, as well as allowing you to parse a String into a date.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahsan H,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use Date's getTime() method to get the number of milliseconds elapsed since Jan 1, 1970 (GMT). From this you can calculate a lot of things, probably more easily than using Celandar. However some calculations are eaiser with Calendar - esp anything that depends on how many days there are in a given month. Generally, I'd try using getTime() first, and then use Calendar methods only if I have to.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
You can also use Date's getTime() method to get the number of milliseconds elapsed since Jan 1, 1970 (GMT). From this you can calculate a lot of things, probably more easily than using Celandar. However some calculations are eaiser with Calendar - esp anything that depends on how many days there are in a given month. Generally, I'd try using getTime() first, and then use Calendar methods only if I have to.

That was my first thought, but depending on how far forward or back you want to calculate, you will have to account for leap seconds and such. I'm assuming GregorianCalendar handles leap time for you. Basically, it depends on what kind of calculations you need to do.
[ January 21, 2003: Message edited by: Jason Menard ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting point. Checking the source code, it doesn't look like GregorianCalendar does anything about leap seconds. Though I may have missed it; I just searched for "leap" or "second" to scan occurances of those words, and didn't see anything that looked like it might handle leap seconds. Researching elsewhere, the Date class documentation notes that dates do not necessarily take leap seconds into account, and thus may be inaccurate by a few seconds. There's also a good paper here which goes into detail about this.
Practically speaking, for most applications this is probably a non-issue. Astronomers and people doing celestial navigation may disagree. But for the rest of us, the worst-case scenario I can imagine is something like this: imagine you're looking for the number of days between two Dates, and you use code like this:

The problem would be if there have been any negative leap seconds introduced between d1 and d2, and the difference is one or more seconds less than some integer multiple of 24*60*60*1000. In this case integer division will round the result towards zero, yielding a difference in days which is 1 less than the expected ("correct") answer. In practice I don't see how this could happen, since all leap seconds so far (and all expected in the forseeable future) are positive, reflecting additional seconds inserted into the day, not removed from it. But if we want to be safe, modify the code a bit:
This forces the division to be done with doubles rather than longs, and rounds the result to the closest integer, rather than rounding towards zero. Which should be more than sufficient to cover up any annoying leap second errors.
[ January 21, 2003: Message edited by: Jim Yingst ]
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting article, thanks. I double checked what I was looking at and absolutely agree that you can't count on leap seconds being handled (although leap years are fine of course). Since there have only been something like 18 leap seconds inserted so far, I agree that there really isn't much point in worrying about it for most applications.
 
Ahsan Hadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim and Jason for your valued suggestions.
Since being a new comer to JAVA, I am in process of transformation at the moment.
What I understand is that I can use the gettime() method from Date object to miillisecond for two dates and then use them for calculation , Can I use settime() to set the date object to my desired dates.
Is is also possible to have two GeorgianCalendar object set to my given dates, could I use them for doing the subtraction. e.g. with the code below
GregorianCalendar beginCalendar = new GregorianCalendar();
beginCalendar.setTime(somedateobject);
GregorianCalendar endCalendar = new GregorianCalendar();
endCalendar.setTime(new Date()); //I want to use current date for this one

long res = endCalendar - beginCalendar // if this does work will it give me answer seconds or days System.out.println("Difference in days: " + res);
regards,
Ahsan.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!