0

I have the following code.

//this line returns - UTC
TimeZone timeZone = TimeZone.getDefault();
//date1 - todays date in UTC format
boolean dstInit = timeZone.inDaylightTime(date1);
//date2 - todays date in UTC format
boolean dstNext = timeZone.inDaylightTime(date2); 

But due to some reason both dstInit and dstNext returning false (instead of TRUE). date1 and date2 is June 20, 2014 in UST formnat which falls in DST time between march and october. Can anyone please help me in fixing this issue?

  • My understanding, and I'm very much open to correction here, is that UTC is a set time, the Daylight savings is seperate e.g. BST is British Summer time and is UTC + 1 (hour forward). So it could just be set at UTC and no DST is involved in the first place. http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices?rq=1 – RossC Jun 20 '14 at 10:31
  • 3
    [UTC](http://en.wikipedia.org/wiki/Coordinated_Universal_Time) does not shift with the seasons; it has no daylight savings period. – Jesper Jun 20 '14 at 10:58

2 Answers2

2

UTC is not a format. It's a system of timekeeping.

  • TAI - Measured from precise atomic clocks

  • UT1 - Measured from the imprecise rotation of the Earth

  • UTC - Calculated by adding leap seconds to TAI to align it to within 0.9 seconds of UT1.

UTC is the common standard we use in computing. A time in UTC can be expressed in a wide variety of formats, however it is always the same time for everyone. It is not affected by time zones, and it never changes for daylight saving time.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • That's true, but also can be misleading. In practice, UTC is also regarded as a timezone (basically equivalent to GMT+0). And, indeed, in Java it's one of the available timezones IDs. – leonbloy Jun 20 '14 at 22:27
  • Yes, UTC can be also thought of as a time zone, but that is more as a matter of convenience for computing. Real places that actually follow UTC year-round still have their own separate time zones - example: `"Atlantic/Reykjavik"`. – Matt Johnson-Pint Jun 20 '14 at 22:30
  • @leonbloy - Also, (sorry for being pedantic) it would be more correct to say that GMT is equivalent to UTC+00:00, rather than the other way around. See also [UTC vs GMT](http://codeofmatt.com/2014/06/18/utc-vs-gmt/). But thanks for calling out the point on UTC as a pseudo-time zone. – Matt Johnson-Pint Jun 20 '14 at 22:33
  • To further complicate this discussion of terms, some operating systems offer "UTC" or "GMT" as a settings choice in the time zone control panel. Indeed, that seems to be the case with the person posting this Question. That's what I deduce from the statement `this line returns - UTC`, the JVM's (computer's) default time zone is so set. – Basil Bourque Jun 21 '14 at 00:42
1
//date1 - todays date in UTC format
boolean dstInit = timeZone.inDaylightTime(date1);

If date1 is a java Date object, then the comment is wrong. A Java Date is a dumb wrapper over a long , it only represents an instant point in the "physical" time, it does not store a Timezone (nor civil date fields), and it has no "format".

Hence, the above line is simply telling if that instant, in that timezone (UTC) corresponds to daylightTime. Of course, it will return false always, because the UTC (pseudo) timezone does not have DST.

leonbloy
  • 73,180
  • 20
  • 142
  • 190