0

My DateFormat is supposed to be correct (24 hour format)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

But I have the weirdest 'bug' when I try to parse (sdf.parse(string))

"2010-03-28 02:28:25"

I get:

Sun Mar 28 03:28:25 CEST 2010

It adds an hour for some weird reason. This is also the only date that gives me this kind of behaviour, other hours etc work just fine. It's always the '02' representing the hour that acts weird.

Any thoughts?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
xrdty
  • 886
  • 2
  • 10
  • 22
  • 3
    I cannot more strongly advise against using the legacy `java.util.Calendar` and `java.util.Date` classes. You should find the appropriate class in the `java.time` package and use that instead. – Joe C Apr 09 '17 at 16:45
  • 2
    As for the bug, my money is on Summer Time v Winter Time. – Joe C Apr 09 '17 at 16:45
  • 3
    It's not, it's because of your timezone – Ashraful Islam Apr 09 '17 at 16:45
  • 4
    What happened, that precise day at 2 o'clock in the morning, in that time zone? https://www.timeanddate.com/time/change/france?year=2010 – JB Nizet Apr 09 '17 at 16:47
  • 1
    @JBNizet I believe the word I'm looking for here is "BINGO!" – Joe C Apr 09 '17 at 16:49
  • 1
    A Sunday this near the end of March? No doubt the Sunday when the at 2 CET am the clock was moved forward to 3 CEST. It is well known that the behaviour of `SimpleDateFormat` in this situation isn’t fully specified. – Ole V.V. Apr 09 '17 at 17:07
  • 1
    Hope this helps. http://stackoverflow.com/questions/10545960/how-to-tackle-daylight-savings-using-timezone-in-java – VNT Apr 09 '17 at 17:08
  • 1
    http://stackoverflow.com/questions/10545960/how-to-tackle-daylight-savings-using-timezone-in-java – VNT Apr 09 '17 at 17:10
  • 1
    http://stackoverflow.com/questions/10545960/how-to-tackle-daylight-savings-using-timezone-in-java – VNT Apr 09 '17 at 17:11

2 Answers2

1

as the comments state, the time you are trying to parse is exactly a Daylight Saving Time (DST) Date, that is a day that the time changed from winter to summer time

the info is here:

https://www.timeanddate.com/time/dst/2010.html

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

There is no such time as 2010-03-28 02:28:25 in your timezone. Due to daylight savings time, the time went from 01:59:59 directly to 03:00:00.

To avoid problems like this, use the new java.time classes and always specify your timezone where appropriate.

String input = "2010-03-28 02:28:25".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;  // Has no time zone nor offset-from-UTC.
ZoneId z = ZoneId.of( "Europe/Paris" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • The locale is irrelevant. What matters here is the timezone. Also, using java.time won't avoid the problem. It will just manifest itself differently. – JB Nizet Apr 09 '17 at 17:01
  • I meant locale, not `Locale`. Changed it anyway. – OrangeDog Apr 09 '17 at 17:03
  • That's why it says "and". The `java.time` package forces you to add the timezone when it is needed. – OrangeDog Apr 09 '17 at 17:04
  • Setting the timezone won't magically make DST disappear. And no, the locale has nothing to do with the timezone (unless we use your specific definition of locale, that has nothing to do with the standard Java definition of locale). – JB Nizet Apr 09 '17 at 17:04
  • 1
    What exactly are you arguing against? The word "locale" just means place where something is located, and I've removed it now to avoid confusion. Using the UTC timezone certainly does make DST disappear. – OrangeDog Apr 09 '17 at 17:08
  • 2
    Being very much a fan of the `java.time` classes, they don’t make summer time (DST) and its associated challenges go away. Their behaviour with summer time is well specified, though, where the docs of the old classes are somewhat vaguer. – Ole V.V. Apr 09 '17 at 17:09
  • Sure, if you parse the date in the wrong timezone, you don't have the same DST problems. But you don't get the correct date either. This is a Java question. A Java locale doesn't have any notion of timezone. It was just a friendly hint to make you change "locale" to "timezone". No need to be so defensive. I didn't even downvote. – JB Nizet Apr 09 '17 at 17:10