1

私のデバイスのタイムゾーンは、ヨルダンのアンマンに設定されています。このコード:

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

なんで?

4

1 に答える 1

0

あなたのコードは問題なく、エラーはまったくありません。ターゲットをクリアして、もう一度確認してください。

こちらをご覧ください

于 2013-01-25T17:14:59.867 に答える