iOS 6 で日付形式指定子が追加されたため、iOS 5 の形式でタイムゾーンを持つ日付を形式ZZZZZ
設定することはできません+99:99
。どちらのバージョンも、. 日付/時刻文字列のタイムゾーンが常にコロンであることがわかっている場合は、コロンを取り除くことができます。+9999
ZZZZ
NSString *unformattedDate = @"2008-09-25T20:41:11.000+00:00";
NSRange range = [unformattedDate rangeOfString:@":" options:NSBackwardsSearch];
if (range.location != NSNotFound && range.location >= unformattedDate.length - 4) {
unformattedDate = [unformattedDate stringByReplacingCharactersInRange:range withString:@""];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:unformattedDate];
[dateFormatter setDateFormat:@"dd.MM.yy"];
NSLog(@"%@", [dateFormatter stringFromDate:dateFromString]);
このような固定フォーマットでは、フォーマッタのロケールを特別な「en_US_POSIX」ロケールに設定する必要があることに注意してください。