DateFormatter引起的问题

昨天老板让我帮忙解决个问题。一个没人在维护的某酒店订票应用的预定日期等很多有日期的地方都显示错误了。具体说就是2012年12月30日以及2012年12月31日显示成了2013年12月30日以及2013年12月31日。很明显就是日期转换成字符串的时候出错了嘛,但具体是什么原因以前还真没遇到过。后面Debug跟了下,发现DateFormatter的dateFormat是"YYYY-MM-dd",与我平时用的"yyyy-MM-dd"的不同在于年份,好吧问题发现了。但具体为啥不同呢?以前真没想过,demo让用这个就用这个了,所以就去查了下class reference,具体区别描述如下:

It uses yyyy to specify the year component. A common mistake is to use YYYYyyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

就是说yyyy指定的是“日历年”,YYYY指定的是“星期年”,具体什么是日历年星期年的没去再找是什么,但打开日历似乎就明白了。所以日后注意DateFormatter里面的年份,你不会想用YYYY的~~~

DateFormatter引起的问题

转载于:https://my.oschina.net/ansonyc/blog/99444