1

I wanted to check the difference in time before and after the daylight saving time started. I am using a JVM where timezone has some issue with DST WRT Brazilian time. I could see the output where it says that 21st October falls under DST, which is wrong as the daylight saving started on 4th of November. I would like to see the timing difference which is 1 Hour in between 20th of October and 21st of October, which I could see in my application because of JRE Timezone issue. Is there any way I can achieve this by a program. Here below the code I have used to see if the date is under DST or not.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Timezone
{
    public static void main(String[] args) throws ParseException {
        TimeZone TIMEZONE = TimeZone.getTimeZone("Brazil/East");

        System.out.println("timeZone : " + TIMEZONE);
        Calendar c = Calendar.getInstance(TIMEZONE);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        dateFormat.setTimeZone(TIMEZONE);

        Date date = dateFormat.parse("2018-10-20T00:00:00");
        c.setTime(date);
        System.out.println("In dst " + c.getTimeZone().inDaylightTime(c.getTime()));

        date = dateFormat.parse("2018-10-21T00:00:00");
        c.setTime(date);

        System.out.println("In dst " + c.getTimeZone().inDaylightTime(c.getTime()));

        date = dateFormat.parse("2018-11-04T00:00:00");
        c.setTime(date);
        System.out.println("In dst " + c.getTimeZone().inDaylightTime(c.getTime()));

    }
}

Adding further to my question I was able to figure out that daylight saving is applied by using my above program. I would like something where it should show me: On 20th October time is 9 am whereas same time due to DST shifted by 1 hr, i.e., 10 am. Is there any way to achieve this?

Expected output:

     timeZone : sun.util.calendar.ZoneInfo[id="Brazil/East"]

         Time is: 21-10-2018 07:52:16

Actual Output faulty JVM:

        timeZone : timeZone : sun.util.calendar.ZoneInfo[id="Brazil/East"]

       Time is: 21-10-2018 08:52:30
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Rajesh Kumar Dash
  • 2,203
  • 6
  • 28
  • 57
  • `java.util.Timezone.inDaylightTime(Date date)` etc covers this. – michaeak Nov 07 '18 at 07:31
  • Possible duplicate of [Determine Whether Daylight Savings Time (DST) is Active in Java for a Specified Date](https://stackoverflow.com/questions/1060479/determine-whether-daylight-savings-time-dst-is-active-in-java-for-a-specified) – michaeak Nov 07 '18 at 07:34
  • Thanks for reply.I could able to know there is change in daylight savings using the above program .But I want to show something like on 20th Oct the time is 9 am where as for 21st Oct it shifted to 10 am due to wrong DST – Rajesh Kumar Dash Nov 07 '18 at 07:34
  • It would be better if you highlight the ACTUAL and the EXPECTED behaviour of your JVM, then it is easier to understand the question. – michaeak Nov 07 '18 at 07:36
  • Added the explanation .. – Rajesh Kumar Dash Nov 07 '18 at 07:43
  • 1
    Is there any reason why you are using the classes `TimeZone`, `Calendar`, `DateFormat`, `SimpleDateFormat` and `Date`? They are all poorly designed and now long outdated. I recommend [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) instead. – Ole V.V. Nov 07 '18 at 12:12

2 Answers2

4

The solution is to fix the JVM with the faulty (outdated) time zone data. It’s pretty straightforward to do using the Time Zone Updater Tool from Oracle. See the link at the bottom (for OpenJDK see the next link).

If you want to know when your JVM “thinks” that summer time (DST) began, you may use the following code:

    ZoneId zone = ZoneId.of("America/Sao_Paulo");
    ZoneRules rules = zone.getRules();
    ZoneOffsetTransition dstStarted = rules.previousTransition(Instant.now());
    System.out.println(dstStarted);

Apparently my Java 11 has the fresh time zone data where summer began on November 4. I got this output:

Transition[Gap at 2018-11-04T00:00-03:00 to -02:00]

“Gap” means that the clock was turned forward as when summer time begins. The transitions were the clock is turned backward are printed as “Overlap”.

If you want, you can also query the ZoneOffsetTransition about the local date and time before and after the transition, the instant of the transition, the UTC offset applied before and after the transition, the signed length of the transition (here 1 hour) and other information.

I am recommending java.time, the modern Java date and time API. The date-time classes that you were using — TimeZone, Calendar, DateFormat, SimpleDateFormat and Date — are all poorly designed and long outdated.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Reminder: One should also update the time zone data in their host operating systems, their database engines such as Postgres, and libraries such as Joda-Time. – Basil Bourque Nov 07 '18 at 16:43
2

I'll add a very brief answer. It shows that 1 day is not equal to 24 hours, i.e. DST changed. I also stepped over to Java 8 because I'm more familiar with it.

ZoneId timezone = ZoneId.of("Brazil/East");

ZonedDateTime t = ZonedDateTime.of(2018, 10, 20, 7, 52, 16, 0, timezone);

System.out.println(t);
System.out.println(t.plus(1, ChronoUnit.DAYS));
System.out.println(t.plus(24, ChronoUnit.HOURS));

Output:

2018-10-20T07:52:16-03:00[Brazil/East]
2018-10-21T07:52:16-02:00[Brazil/East]
2018-10-21T08:52:16-02:00[Brazil/East]

Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50