简体   繁体   中英

Odd behavior of SimpleDateFormat

Code:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMdd", Locale.US);
Object o = simpleDateFormat.parse("20111216");
System.out.println(simpleDateFormat.format((Date)o));

Output

20111226

Why? Giant bug?

it should be

yyyyMMdd

Note: small letter y

See Also

I think the greater question hinted by the op on this has been missed.

Why is it that sometimes 'Y' works despite the docs. Yes it should be 'y', but on some systems 'Y' works.

I developed on a mac where 'Y' worked and it blew up on prod Linux where 'Y' didn't work. Not hard to realize what's going on, but very annoying.

一旦我修复了模式(应该是yyyyMMdd并使用小写y ),代码就会按预期工作(输出20111216 )。

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