SimpleDateFormat("dd-MMM-YYYY")打印年份提前一年 [英] SimpleDateFormat("dd-MMM-YYYY") printing year one year ahead
问题描述
我在我的代码中使用了SimpleDateFormat("dd-MMM-YYYY"),输出错误.
I am using SimpleDateFormat("dd-MMM-YYYY") in my code, which is giving wrong output.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-YYYY");
System.out.println("Actual date : "+new Date()+" after Formatting : "+ dateFormat.format(new Date()));
上面的代码给出了: 实际日期:格式化后的IST 2014年12月30日星期二13:51:06 2015年12月30日
above code is giving : Actual date : Tue Dec 30 13:51:06 IST 2014 after Formatting : 30-Dec-2015
上面的代码是打印日期,其中Year提前1年. 并且此问题仅可复制到2014年12月28日至31日.
Above code is print date having Year as 1 year ahead. and this issue is replicable for 28-31 december 2014 dates only.
先谢谢了. --Ajay
Thanks in advance. --Ajay
推荐答案
您正在使用YYYY
,它是"ISO-8601周年".几乎应始终将 与w
(每年的一周)结合使用.您希望yyyy
显示正常的日历年.
You're using YYYY
, which is the "ISO-8601 week year". That should almost always be used in conjunction with w
, "week in year". You want yyyy
to show the normal calendar year.
它们不同的原因是一年中的第一个ISO-8601周是包含至少4天的第一个(周一至周日)周.这意味着一年的第一周就是包含第一个星期四的一周.由于2015年1月1日是星期四,这意味着2014-12-29到2015-01-04的一周都是"2015年,第一年的一周". (如果您看到是否在12月28日...,我会感到很惊讶.)
The reason they're different is that the first ISO-8601 week of a year is the first (Monday to Sunday) week that includes at least 4 days. This means that the first week of the year is the one containing the first Thursday. As January 1st 2015 falls on a Thursday, that means that the week of 2014-12-29 to 2015-01-04 is all "week year 2015, week of year 1". (I'm surprised if you see if for December 28th...)
在其他年份,一年的前几天在上一年的第52周或第53周.例如,2010年1月1日位于2009年第53周,而2011年1月1日位于2010年第52周.
In other years, the first few days of the year are in week 52 or 53 of the previous year. For example, January 1st 2010 was in week 53 of week-year 2009, and January 1st 2011 was in week 52 of week-year 2010.
这篇关于SimpleDateFormat("dd-MMM-YYYY")打印年份提前一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!