我正在创建这样的日期:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY"];
NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue];
NSLog(@"NSInteger YEAR : %d\n", year);
//minutes, hours, day and month are initialized elsewhere...
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:year];
[comps setMonth:month];
[comps setDay:day];
[comps setHour:hours];
[comps setMinute:minutes];
NSDate *sampleDate = [gregorian dateFromComponents:comps];
NSLog(@"sampleDate YEAR : %d\n", [[dateFormatter stringFromDate:sampleDate] intValue]);
我的问题是,当日期是 1 月 1 日或 1 月 2 日时,年份不正确。这里应该是 2010 年(当我检索今天的年份时),但实际上是 2009 年......我不明白为什么!而且这种情况只发生在那两天......
你知道为什么会这样吗?
先感谢您。
See the 统一码标准。 “yyyy”(小写)为您提供年份,但“YYYY”(大写)为您提供该日期所在周的年份(也就是说,如果该日期在去年的第 52 周内,则 YYYY 将是去年)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)