Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get actual DST savings in milliseconds

I'm making analog clock in Java Swing. To calculate clock pointer angles I need:

  1. UTC time in milliseconds which I can get with System.currentTimeMillis()
  2. TimeZone offset
  3. TimeZone DST savings.

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
like image 562
zlatanmomic Avatar asked Nov 08 '10 16:11

zlatanmomic


People also ask

What is the function to offset the date for daylight saving?

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.

Does Javascript date handle daylight Savings?

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.

How does Java handle daylight saving?

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.

What is the function to offset the date for daylight saving in time series Javascript?

To check if DST (Daylight Saving time) is in effect: Use the getTimezoneOffset() method to get the timezone offset for the 2 dates.


2 Answers

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.

like image 130
dogbane Avatar answered Oct 13 '22 04:10

dogbane


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.

like image 29
DaGGeRRz Avatar answered Oct 13 '22 05:10

DaGGeRRz