Try the following code below. It is the same as your code but with
these extensions:
a) Explicity set the timezone as "America/New_York"
b) Explicitly set the DST_OFFSET to make your intention clear that you
want 1AM to mean before or after DST switch.
I tested it by changing my system clock to different times and it always works.
private static void testDiffBetweenTimesDuringDSTSwitch() {
Calendar calendarStart = Calendar.getInstance();
calendarStart.setTimeZone(TimeZone.getTimeZone("America/New_York"));
calendarStart.set(2007, Calendar.NOVEMBER, 3, 18, 0);
Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTimeZone(TimeZone.getTimeZone("America/New_York"));
calendarEnd.set(2007, Calendar.NOVEMBER, 4, 1, 0);
calendarEnd.set(Calendar.DST_OFFSET, 3600000);
printTimeDiff(calendarStart, calendarEnd);
}
/**
* Assumes the given dates have 0 minutes and seconds components.
*/
private static void printTimeDiff(Calendar calendarStart,
Calendar calendarEnd) {
long startMillisec = calendarStart.getTimeInMillis();
long endMillisec = calendarEnd.getTimeInMillis();
long diff = endMillisec - startMillisec;
long diffHours = diff / 3600000;
System.out.println("diffHours --> " + diffHours);
System.out.println("Start date DST Offset --> "
+ calendarStart.get(Calendar.DST_OFFSET));
System.out.println("End date DST Offset --> "
+ calendarEnd.get(Calendar.DST_OFFSET));
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden
This email sent to email@hidden