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.
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 ]
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 ]
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();
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);
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Rocket Oven Kickstarter - from the trailbosshttps://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss