我的设备时区设置为约旦安曼。这段代码:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormatter dateFromString:@"20130329"];
NSLog(@"date: %@, timezone: %@", date, [NSTimeZone systemTimeZone]);此日志中的结果:
date: (null), timezone: Asia/Amman (GMT+02:00) offset 7200大概是因为约旦安曼的夏令时,这个日期和时间不存在。但是下面的代码:
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
components.year = 2013;
components.month = 03;
components.day = 29;
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents:components];
NSLog(@"date: %@, timezone: %@", date, [calendar timeZone]);此日志中的结果:
date: 2013-04-28 21:00:00 +0000, timezone: Asia/Amman (GMT+02:00) offset 7200为什么?
发布于 2013-01-26 01:15:00
你的代码是好的,没有任何错误,所以,清除目标并再次检查。
请参阅here
https://stackoverflow.com/questions/14526480
复制相似问题