Date formatting error when it is on Brazilian daylight saving time (BRST)

Asked

Viewed 2,601 times

1

I have a problem when removing from a date variable (Sun Oct 21 01:00:00 BRST 2018) that one hour difference, because it is generating a billing error, when the date is footprint of the database with that 1 hour difference.

I’ve tried using the conversion.

public static Date zeroTimes( final Date data ) {
    Calendar cal = Calendar.getInstance();
    cal.setTime( data );
    cal.set( Calendar.HOUR_OF_DAY, 0 );
    cal.set( Calendar.MINUTE, 0 );
    cal.set( Calendar.SECOND, 0 );
    cal.set( Calendar.MILLISECOND, 0 );
    return cal.getTime();
}

Only I never get the expected return that would be Sun Oct 21 00:00:00 BRST 2018.

Does anyone have any idea how to get it out, because I’ve tried it in many ways.

  • How about messing with new and optimized classes for this instead of using Calendar and Date?

  • To make this much easier with new classes, there is a restriction of java-8 or date and even date?

  • I know that, only that the libraries used in the company can not be changed, and also can not use Joda time

  • I ran your code and the return was as expected by you: Thu May 03 00:00:00 BRT 2018

1 answer

1

Your JVM must still have the old rules of daylight savings time. Until 2017, Brazilian daylight saving time began on the third Sunday in October, but in December 2017 the government changed the rule and from 2018, the start will be on the first Sunday of November.

As these settings are in the JVM, she still thinks that daylight saving time will start on October 21, 2018. Proof of this is the "BRST", which is the abbreviation of "Brasília Summer Time" (another name for Brazil’s daylight saving time).

To fix, you must update the JVM time zone information. There are 2 ways to do it:

  1. Update the Java version. On this page you can see that versions 10, 8u172, 7u181 and 6u191 have the tzdata2018c, which is the version that has the new rules of Brazilian daylight saving time (see fourth column: "Brazil’s DST will now start on November’s first Sunday"). Just upgrade Java to one of these versions.

  2. Update only the tzdata of the JVM (the time zone information), without changing the Java version. For this, you need to run the TZ Updater Tool. One of the arguments of this command is the file you will use, and the same can be found in https://www.iana.org/time-zones

    The latest file can always be downloaded at https://data.iana.org/time-zones/tzdata-latest.tar.gz. You can download it and run the command as instructed. But always check the iana’s website to find out if there is a newer file. In this question there is a more detailed explanation on how to do this update, and at this link are all versions, if you need any earlier than the current.

My JVM has already been updated, so the output of your code to me is:

Sun Oct 21 00:00:00 BRT 2018

Note that it is no longer "BRST", but "BRT", indicating that it is not daylight saving time.


Without updating the time zone information, it is not possible to set the time to midnight on the 21st, because of how daylight saving time works.

When daylight saving time starts, the following happens: By the time midnight arrives, the clock is automatically advanced by one hour. Literally "we jumped" an hour: every minute between 00:00 and 00:59 there are no on this day.

Currently computers and mobile phones already have these settings and change automatically. The clock jumps from 23:59:59 direct to 01:00. So when daylight saving time starts, there is no midnight on that day, and the API adjusts to the next valid time (in this case, 01:00).

And since her JVM still has the old rules, she thinks daylight saving starts on October 21st, and so it’s not possible to set the time to midnight on this day.


Detail: after upgrading the JVM, the issue of day 21 will be fixed. However, this situation will still occur on 4 November, when daylight saving time begins. No use, there will always be some day when the clock is early and certain times do not exist in that Timezone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.