Java显示错误的当前年份

4
我已经编写了打印当前日期的代码。
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("ddMMYYYY");
String newdate = dateFormat.format(date);

但它的输出为:29122016
实际输出应该为:29122015
系统日期也是正确的。
代码有什么问题?

谢谢!它起作用了 :) - Mosam Mehta
1个回答

11

Y代表周年。您应该更改为:

DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");

SimpleDateFormat提供了详细信息:

Letter | Date or Time Component | Presentation | Examples
-------+------------------------+--------------+----------
y      | Year                   | Year         | 1996; 96
Y      | Week year              | Year         | 2009; 09

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接