我的设备时区设置为约旦安曼。这段代码:
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
假设由于约旦安曼的 DST,这个日期和时间不存在。但随后这段代码:
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
为什么?