3

I used a calander and add one minute to it each time. but in date "2017-9-21 23:59" something strange happent. The date come one hour back. It's behavior is like date saving time, but that time date saving must not happent.

here is my code and output:

GregorianCalendar fromCalendar = new GregorianCalendar(2017, 8, 21, 22, 58);

    for (int i = 0; i < 120; i++) {
        System.out.println(fromCalendar.get(Calendar.YEAR) + "-"
                + (fromCalendar.get(Calendar.MONTH) + 1) + "-" +   fromCalendar.get(Calendar.DAY_OF_MONTH) + " "
                + fromCalendar.get(Calendar.HOUR_OF_DAY) + ":" + fromCalendar.get(Calendar.MINUTE) + "      ");
        fromCalendar.add(Calendar.MINUTE, 1);
    }

Output:

.
.
.
2017-9-21 23:58     
2017-9-21 23:59     
2017-9-21 23:0      
2017-9-21 23:1      
2017-9-21 23:2  
.
.
.

Is there any simple point I misunderstood about it?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Narges
  • 1,345
  • 6
  • 14
  • 29
  • 3
    Why do you think no DST change must happen then? – marekful Jan 09 '18 at 06:37
  • 2
    What is your timezone? Your code works fine for me in UTC-07:00 – Jim Garrison Jan 09 '18 at 06:39
  • 5
    I cannot more strongly advise against using the legacy `java.util.Calendar`. You should instead look at the `java.time` package and select the class appropriate for your use case. – Joe C Jan 09 '18 at 06:42
  • 4
    FYI, the troublesome old date-time classes such as `GregorianCalendar`, [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 09 '18 at 07:25
  • You should not have @JimGarrison tell you which time zone your date-time object is using. Instead you should tell it which time zone it should use. If you want to avoid any and all DST transitions, one solution is to use UTC, for example `OffsetDateTime.of(2017, 8, 21, 22, 58, 0, 0, ZoneOffet.UTC).plusMinutes(1).plusMinutes(1)`. – Ole V.V. Jan 09 '18 at 07:31

2 Answers2

10

According to the TimeAndDate.com DST page there is only ONE country in the world where the DST changeover happened at 00:00 on 2017/09/22: Iran

Therefore you must be using the timezone for Iran: Asia/Tehran.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
3

The Answer by Jim Garrison is correct and should be accepted. You are apparently seeing a Daylight Saving Time cut-over for a time zone such as Asia/Tehran.

java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

Specify a time zone using ZoneId to get a ZonedDateTime object.

ZoneId z = ZoneId.of( "Asia/Tehran" ) ;  // Testing the Daylight Saving Time cut-over in Iran.
ZonedDateTime zdt1 = ZonedDateTime.of( 2017 , 9 , 21 , 23 , 59 , 0 , 0 , z );  // Set the moment to what might *appear* be the minute before midnight but is not, is actually an hour and a minute before midnight. 
ZonedDateTime zdt2 = zdt1.plusMinutes( 1 );  // Adding a minute takes us to 11 PM again, but with a different offset-from-UTC, for a 25-hours long day. 

Dump to console.

System.out.println( "zdt1: " + zdt1 );
System.out.println( "zdt2: " + zdt2 );

See this code run live at IdeOne.com.

zdt1: 2017-09-21T23:59+04:30[Asia/Tehran]

zdt2: 2017-09-21T23:00+03:30[Asia/Tehran]

Notice how adding a minute to roll-over midnight sent us back to 11 PM on the same date but with a different offset-from-UTC, 03:30 versus 04:30. So the 21st of September 2017 in Iran ran 25 hours long.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154