简体   繁体   English

SimpleDateFormat的奇怪行为

[英]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 注:小写字母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. 为什么有时尽管文档,但“ Y”仍然有效。 Yes it should be 'y', but on some systems 'Y' works. 是的,应该为“ y”,但在某些系统上,“ Y”有效。

I developed on a mac where 'Y' worked and it blew up on prod Linux where 'Y' didn't work. 我在Mac上开发了“ Y”的工作原理,然后在生产Linux的“ Y”不工作的情况下崩溃了。 Not hard to realize what's going on, but very annoying. 不难发现发生了什么,但是很烦人。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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