3

We faced following problem:

    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy", java.util.Locale.GERMAN);
    String dateInString = "06-04-1980";
    Date date = formatter.parse(dateInString);

before: Sun Apr 06 00:00:00 CEST 1980

after: Sun Apr 06 01:00:00 CEST 1980

Another example:

Date date = Date.from(LocalDate.of(1980, 4, 6).atStartOfDay(ZoneId.systemDefault()).toInstant());

Facing the same problem.

We thought change of java11 to java17 is the problem, but it wasn't. It was the change of the timezone from Europe/Berlin to Europe Vienna. On 1980-04-06 the daylight saving time was established in Austria and the change of the hour was at 00:00. So there was no 00:00 at this date.

Reproduceable example - changing timezone to "Europe/Berlin" results in 0 hour.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
    for (LocalDate date = LocalDate.of(1500, 04, 01); date.isBefore(LocalDate.of(9999, 1, 1)); date = date.plusDays(1)) {
        Date out = Date.from(date.atStartOfDay(ZoneId.of("Europe/Vienna")).toInstant());
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone(ZoneId.of("Europe/Vienna")));
        cal.setTime(out);
        if (cal.get(Calendar.HOUR_OF_DAY) > 0) {
            System.out.println(date.format(formatter) + " > " + cal.get(Calendar.HOUR_OF_DAY));
        }
    }
    System.out.println("done");

All dates before 1893-03-31 have 23 as hour in timezone "Europe/Vienna", in "Europe/Berlin" its also 0.

24
  • 3
    What if you use LocalDate/the java.time API instead? Commented Aug 11, 2022 at 5:32
  • 6
    And what is the question now? What do you think is the right answer and why? What do you want to happen and why? Commented Aug 11, 2022 at 5:37
  • 2
    I think you misunderstood java.util.Date - it does not have an "hour". It represents an instant in time. The "hour" you see depends on how you print it out. Commented Aug 11, 2022 at 6:12
  • 2
    Don't quite understand your setup - minimal reproducible example please, demonstrating the Because this date differs from old ones in our database - so old dates could not be found Commented Aug 11, 2022 at 10:38
  • 3
    A guess, by mistake you have your Java 11 use Europe/Berlin time zone as its default and your Java 17 use Europe/Vienna. As I said, I have not been able to reproduce. Also if you are using Europe/Vienna, then as I read the error was on Java 11, so if you move on to Java 17, it's gone and the problem is solved, is this correct? Commented Aug 12, 2022 at 9:34

3 Answers 3

3

It's not really a problem, it's a special thing about timezone "Europe/Vienna" which was changed in our system. If you get this problem, check your timezone, maybe it was changed by some other properties.

While summer time (DST) started at 02:00 o’clock in Germany on that date, it started already at midnight in Austria (Europe/Vienna time zone), so the time 00:00 did not exist, which is why we suddenly got 01:00. Both time zones are printed as CEST (Central European Summer Time), so the abbreviation does not allow us to distinguish.

Sign up to request clarification or add additional context in comments.

1 Comment

The reason saving time adjustments usually take place at 2:00 is to move the odd behaviour off the day boundary. I'm surprised that lesson wasn't learned before 1980!
0

I am in Israel, so in order to simulate the time zone of central Europe (using Java 17), I set the [System] property user.timezone to CET using the -D option of the java command. Then I ran the following code:

/*
 * import java.time.ZoneId;
 * import java.time.ZonedDateTime;
 * import java.util.Date;
 */
ZonedDateTime zdt = ZonedDateTime.of(1980, 4, 6, 0, 0, 0, 0, ZoneId.systemDefault());
System.out.println(Date.from(zdt.toInstant()));

The result I got was:

Sun Apr 06 00:00:00 CET 1980

This is the result that you want, correct?

6 Comments

Yes, that's the result I want, but if I run your code I get: Sun Apr 06 01:00:00 CEST 1980
@anm note CET in @Abra output - you are "expecting" CEST, however I always getting Sun Apr 06 01:00:00 CEST 1980 regardless java version.
I tried same Code in different Java Versions and get differences. I recognized CET/CEST.
@AndreyB.Panfilov When I call ZoneId.getAvailableZoneIds() it does not contain CEST but it does contain CET.
I think the Java versions I've used have translated CET to Europe/Paris (not completely sure). Not really helpful for a problem with either Austria or Germany time zone.
|
0

In my opinion, the Java 1.8 implementation has another issue with "Europe/Vienna". Yes, the time zone "Europe/Vienna" is special at 1980-04-06 in that the clock change happens at the first hour, so this day effectively starts at 1 o'clock local time, not at 0 o'clock as, to the best of my knowledge, anywhere else in the world even at the days of spring clock changes. But that's how Austria wanted it to happen back then, and that's how "Europe/Vienna" has been defined in the TZ / Olson database. This is what Java correctly shows, given the time zone has been set to "Europe/Vienna".

The real issue comes into play if you issue an Calendar.add(Calendar.DAY_OF_MONTH, 1), of course after having adjusted the Calendar instance to "Europe/Vienna". I'd expect the first hour of the next day to be 0 in local, daylight saving time, but Java computes 1, which is wrong.

3 Comments

isn't thar already given in the 3 year old accepted answer?
My understanding of that answer is that it is about 1980-04-06 in Austria, not about 1980-04-07, which my statement is about. I explicitly confirmed that Java correctly prints the first hour of 1980-04-06 in local time "Europe/Vienna", but adding another day in my opinion Java prints the wrong first hour—Calendar.get(Calendar.HOUR_OF_DAY) still returns 1, but should return 0 in local time "Europe/Vienna".
I disagree. First, never use Calendar. Use java.time, the modern Java date and time API. Second, adding one day to a date and time at 1 AM on one day should give what time of day the next day? You say 0, and I find no reasonable argument in favour of that. It should of course yield 1 if the time 1 AM exists on that day in that time zone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.