7

Let's consider the following code:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.US);
long start = sdf.parse("10:30:00 30/09/2009").getTime();
long end = sdf.parse("10:30:00 30/10/2009").getTime();

Calendar c = Calendar.getInstance(Locale.US);
c.setTimeInMillis(start);
System.out.println("Start = " + c.getTime());
c.setTimeInMillis(end);
System.out.println("  End = " + c.getTime());

When running this code snippet, I have the following output:

Start = Wed Sep 30 10:30:00 CEST 2009
  End = Fri Oct 30 10:30:00 CET 2009

Why do I get different timezone ?

Note that if I set the first date in august and the second one in september, the output will display the same timezone in both cases:

long start = sdf.parse("10:30:00 30/08/2009").getTime();
long end = sdf.parse("10:30:00 30/09/2009").getTime();

will display:

Start = Sun Aug 30 10:30:00 CEST 2009
  End = Wed Sep 30 10:30:00 CEST 2009

I'm using Java 1.6.0_14

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
  • Is it related to the winter time switch in end of october? If yes, how to get rid of this? – Romain Linsolas Jan 19 '10 at 09:19
  • You don't. You can force the `CEST` to `CET` but that will result in a different hour (the October one will be 11:30 instead of 10:30). It's the same as GMT (well, UTC now) and London time: when DST is on, London Time is technically GMT + 1, CET is GMT + 2 etc – laura Jan 19 '10 at 09:26
  • What exactly do you want to achieve? *Why* do yo think you need to get rid of the DST? – Michael Borgwardt Jan 19 '10 at 09:32
  • Doesn't anyone wonder while the timezone `CET` (European) is returned for `Locale.US` or what am I missing here? – Risadinha Oct 20 '16 at 15:26

3 Answers3

9

CEST is Central European Summer Time. It is the same as CET with daylight savings into effect.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • That's what I suspected, but then how can I get rid of this daylight saving time? – Romain Linsolas Jan 19 '10 at 09:23
  • Why do you want to get rid of it? CET is -01:00 whereas CEST is -02:00 (hours of difference from GMT time, for which daylight savings are never applied). This the convention for timezones and you shouldn't break it. What exactly are you trying to achieve? Is this a presentation or a calculation issue? – kgiannakakis Jan 19 '10 at 09:27
  • Correction: CET is +1 and CEST is +2 – Zoltán Jan 04 '14 at 18:17
  • Where can I find some reference doc that lists all time zones available in java, where it would mention this CEST thing? CEST doesn't seem to exist anywhere in official timezone docs (CET does exist), ex: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones Are there any other "extra" timezone like this one? – Simon Mourier Sep 27 '19 at 16:13
7

You can set the default time zone

    import java.util.TimeZone;
...        
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));  // or "Etc/GMT-1"

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.US);
    long start = sdf.parse("10:30:00 30/09/2009").getTime();
    long end = sdf.parse("10:30:00 30/10/2009").getTime();

    Calendar c = Calendar.getInstance(Locale.US);
    c.setTimeInMillis(start);
    System.out.println("Start = " + c.getTime());
    c.setTimeInMillis(end);
    System.out.println("  End = " + c.getTime());

use TimeZone.getAvailableIDs() to see all available IDs.

EDIT: you can also use a new SimpleTimeZone

    TimeZone.setDefault(new SimpleTimeZone(60 * 60 * 1000, "CET"));
user85421
  • 28,957
  • 10
  • 64
  • 87
0

Yes, it is related to the daylight saving time. If you use a time zone that recognizes DST, it will be automatically used. You can use GMT for example if you don't want this.

nanda
  • 24,458
  • 13
  • 71
  • 90