我有这段代码要阅读 ISO 8601 日期/时间规范。问题是由于某种原因,这个月不起作用。每次我转换它的月份都是“01”。我究竟做错了什么?
NSDateFormatter *UTCDateFormat = [[NSDateFormatter alloc] init];
[UTCDateFormat setDateFormat:@"yyyy-MM-DD'T'HH:mm:ss'Z'"];
[UTCDateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[UTCDateFormat setCalendar:[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]];
[UTCDateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
NSLog(@"%@", [UTCDateFormat dateFromString:dateString]);
因此,例如,如果 dateString 是:
@"2002-03-12T21:13:27Z"
我的输出是:
2002-01-12 21:13:27 +0000
我已经尝试过的事情:
- 将 MM 更改为 LL
- 尝试不使用 setCalendar/setLocale
- 尝试使用 ' 围绕 -
- 把MM的位置调换到其他地方。它永远不会改变任何东西。
- 到处查看,它似乎对其他人有用。