私の iOS アプリでは、現在の日付に基づいて週を作成する必要があります。
これはコードの一部です:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:year];
[components setWeek:week];
[components setWeekday:2];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
この特定のケースでは、年は 2012 年で、週は 1 です。2012 年第 1 週の月曜日の日付であるため、カレンダーは 2012-01-02 を返すと予想します。しかし、代わりに 2012-12-30 を返します。
タイムゾーンで更新:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2012];
[components setWeek:1];
[components setWeekday:2];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [gregorian dateFromComponents:components];
それでも 2012-12-30 を返します。