I'm making analog clock in Java Swing. To calculate clock pointer angles I need:
System.currentTimeMillis()
For 2) and 3) I thought of using
TimeZone.getDefault().getRawOffset()
and TimeZone.getDefault().getDSTSavings()
.
But getDSTSavings()
always returns 3600000
regardless of current date is in winter/summer time.
It looks like it only checks to see if that zone is under DST
management and if so it returns 3600000
otherwise 0
.
Is this a bug in getDSTSavings()
?
If not then only way to get current DST
correction in miliseconds is to use Calendar instance like: Calendar.getInstance.get(Calendar.DST_OFFSET)
?
Calendar cal1 = Calendar.getInstance(TimeZone.getDefault()); //currentZone: CET/CEST +1/+2, GMT+1:00
System.out.println("System time, " + System.currentTimeMillis()); //UTC current milis
System.out.println("Calendar time, " + cal1.getTime().getTime()); //UTC current milis
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC current milis
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET));
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET));
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset());
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings());
System.out.println("");
// Winter time, CET
cal1.set(2010, 11, 15, 14, 15, 5);
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET)); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET)); // 0 correct
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset()); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings()); // 3600000 wrong !!!
System.out.println("");
// Summer time - CEST
cal1.set(2010, 8, 15, 14, 15, 5);
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET)); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET)); // 3600000 correct
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset()); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings()); // 3600000 correct
IsDaylightSavingTime(DateTimeOffset) Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.
The answer is Yes. If we are considering New York, USA, then during Daylight Saving, the offset is -4, whereas it would otherwise be -5. There's a solution for this as well: an additional function while obtaining destination city's UTC offset.
The right way to handle DST in Java is to instantiate a Timezone with a specific TZDB Timezone ID, eg. “Europe/Rome”. Then, we'll use this in conjunction with time-specific classes like java. util.
To check if DST (Daylight Saving time) is in effect: Use the getTimezoneOffset() method to get the timezone offset for the 2 dates.
Here is an example of how DST is used:
TimeZone london = TimeZone.getTimeZone("Europe/London");
System.out.println(london.getOffset(date.getTime()));
This will print 3600000 if Daylight Saving Time is in effect in London at the specified date. Otherwise it will print 0.
The cal1 instance the you retrieve on the first line is in GMT+1 and that does not change regardless of what time / date you set on it. If you need to check the current timezone on every tick of the clock, you will need to retrieve a new Calendar instance at each tick.
As a side-note, if you're willing to add a dependency to your project - Joda Time is a wonderful Date and Time library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With