Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calendar difference adds extra 12 hours in java 6

807589Aug 20 2008 — edited Aug 20 2008
Hi, I am using simple java calendar code to get the time difference between 2 sets of date/times with one date being an hour more than the first date but into the next day.
The result is adding 12 hours instead - any help?

Java is java version "1.6.0_07"

//date-time = 12/1/2007 11:30:00 PM
Calendar startDateTime = Calendar.getInstance();
startDateTime.clear();
startDateTime.set(2007, 12, 1, 11, 30, 00);
startDateTime.add(Calendar.AM_PM, Calendar.PM);

//date-time = 12/2/2007 12:30:00 AM
Calendar endDateTime = Calendar.getInstance();
endDateTime.clear();
endDateTime.set(2007, 12, 2, 12, 30, 00);
endDateTime.add(Calendar.AM_PM, Calendar.AM);

long start = startDateTime.getTimeInMillis();
long end = endDateTime.getTimeInMillis();
long diff = end - start;

System.out.println("diff: " + diff);

The result difference is 46800000 milliseconds difference which is 13 hours even though it is really only 1 hour difference.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 17 2008
Added on Aug 20 2008
8 comments
814 views