简体   繁体   中英

Inconsistent behavior in java date formatter

Either I'm missing something completely obvious or making unwarranted assumptions about the behavior of SimpleDateFormat.

I'd assume that it ought to be symmetric, right? If I have

SimpleDateFormatter formatter;
String datestr=...
Date d = formatter.parse(datestr);
String s=formatter.format(d);

I should get back my original String. However -

public static final String DATE_FORMAT = "E MMM dd HH:mm:dd zz yyyy";
String test = "Mon Nov 29 09:53:24 EST 2010";
long l = new SimpleDateFormat(DATE_FORMAT).parse(test).getTime();
System.out.println(test);
System.out.println(new SimpleDateFormat(DATE_FORMAT).format(new Date(l)));

prints out :

Mon Nov 29 09:53:24 EST 2010

Wed Nov 24 09:53:24 EST 2010

How am I losing 5 days?

"E MMM dd HH:mm:dd zz yyyy";
       ^^       ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM