Take the 2-minute tour ×

This code:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD_HH-mm-ss");
Date date=null;
try {
  date = format.parse("2012-07-04_13-42-03");
} catch (Exception e) {
File file = new File("File.txt");
Date now=new Date();
Date last= new Date(file.lastModified());
System.out.println("exec:"+date+" "+date.getTime()+" "+date.getTimezoneOffset());
System.out.println(" now:"+now+" "+now.getTime()+" "+now.getTimezoneOffset());
System.out.println("last:"+last+" "+last.getTime()+" "+last.getTimezoneOffset());

gives the result:

exec:Wed Jan 04 13:42:03 CET 2012 1325680923000 -60
 now:Wed Jul 04 13:42:20 CEST 2012 1341402140349 -120
last:Mon Jul 02 17:26:37 CEST 2012 1341242797000 -120

How can I create the date from the string so I can compare It with the rest, in the same timezone?

share|improve this question
Is the code and output right? It seems to be in a different order, plus last and exec call the same code (date.getTime()) but return different results? –  Disco 3 Jul 4 '12 at 11:59
I've taken the order from another place, where it prints multiple dates. –  magodiez Jul 4 '12 at 12:02
Is a recop of different code, sorry. Problems edited. –  magodiez Jul 4 '12 at 12:04
btw you should have used dd instead of DD: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); –  Andrew Butenko Jul 4 '12 at 12:16
thanks, just found out, and then read your comment... –  magodiez Jul 4 '12 at 12:22
add comment

2 Answers

Date doesn't have a time zone. What's confusing you is that Date.toString() always returns a value formatted in the system default time zone, and that's what Date.getTimeZoneOffset does as well.

Now, in your system default time zone (Central European time, by the looks of it) the offset from UTC changes over the course of the year, as does the time zone abbreviation. That's what you're seeing: the difference between "Central European" time (UTC+1) and "Central European Summer Time") (UTC+2). That's the time zone information in the current system time zone for the instant in time represented by the Date value you're converting into a string representation.

Again, the Date itself has no information about the time zone. It's just a point in time. You can set the time zone that the SimpleDateFormat uses when parsing or formatting - that's a different matter.

You should also consider using Joda Time if at all possible - it's much better than the API built into Java.

share|improve this answer
I was going to answer about the time zone difference in January and July and link to one of your answer stackoverflow.com/questions/6841333/… –  raychenon Jul 4 '12 at 12:05
add comment
up vote 1 down vote accepted

Ok, so the problem wasn't the timezone, but the conversion from



Wed Jan 04 13:42:03 CET 2012

the problem is in the format pattern so changing




since D means Day in year and d means Day in month

share|improve this answer
add comment

Your Answer


