Jon Ander Jon Ander - 1 year ago 82
Java Question

Java getting UTC+2 when it would be UTC+1

Last sunday we change the time (-1h) in middle europe. But I was making some tests and something was wrong with the java time parser. This is the code

public static void main(String[] args) {
String dateFormatPattern = "yyyy-MM-dd HH:mm:ss";
String dateUtc = "2016-10-09 12:50:00";

SimpleDateFormat dateFormatUtc = new SimpleDateFormat(dateFormatPattern);

SimpleDateFormat dateFormatLisboa = new SimpleDateFormat(dateFormatPattern);

SimpleDateFormat dateFormatMadrid = new SimpleDateFormat(dateFormatPattern);

SimpleDateFormat dateFormatParis = new SimpleDateFormat(dateFormatPattern);

System.out.println("UTC: "+dateUtc);
try {
Date d = dateFormatUtc.parse(dateUtc);
System.out.println("Lisboa: "+dateFormatLisboa.format(d));
System.out.println("Madrid: "+dateFormatMadrid.format(d));
System.out.println("Paris: "+dateFormatParis.format(d));
} catch (ParseException e) {

And this is the output

UTC: 2016-10-09 12:50:00
Lisboa: 2016-10-09 12:50:00
Madrid: 2016-10-09 14:50:00
Paris: 2016-10-09 14:50:00

Why the difference between UTC and Madrid time are 2 hours? Now in madrid is UTC+1.


Answer Source

The times are correct as the clocks changed on the 30th October at 2am

if you change you code to this

String dateUtc = "2016-11-09 12:50:00";

You get this output, giving the correct 1 hour difference.

UTC: 2016-11-09 12:50:00
Lisboa: 2016-11-09 12:50:00
Madrid: 2016-11-09 13:50:00
Paris: 2016-11-09 13:50:00

The timezone is due to the when the date object is actually referencing. So it is correct for that time

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download