I’m seeing a problem with NSDateFormatter on iOS when Settings/General/International/Calendar is set to either Japanese or Buddhist on both the simulator and a real device. The year is not parsed correctly
DateFormatter
static NSDateFormatter *formatter = nil; // cache this the first time through
if (formatter == nil) {
formatter = [NSDateFormatter new];
formatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; // Fix for QC79748 - Michael Marceau
formatter.dateFormat = @"EEE, d MMM yyyy HH:mm:ss zzz";
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
formatter.calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
formatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]] autorelease];
}
NSLog(@"CorrectDate%@",[serviceHeaders safeObjectForKey:@"Date"] );
NSLog(@"Formatted date:%@",[formatter dateFromString:[serviceHeaders safeObjectForKey:@"Date"]]);
Output
Correct - Mon, 27 Aug 2012 16:33:14 GMT
Formatted date:0024-08-27 16:33:14 +0000
This is working just as it should. I took your code and added it to my project, then set the Simulator to use the Buddhist calendar.
Then the output:
Analysis:
Its all working perfectly. In the Buddhist calendar, the year is 2555 now. When you provide a Gregorian date, and ask the formatter to use the Gregorian calendar, it reads it in properly, then converts it to the Buddhist date, and when you print that out, the date is 2555 again. Just what you would expect.
EDIT: Just to press the point, the NSDate is always the same, what changes is the representation of it. So I set the calendar to Buddhist again, and used your formatter to get the current time in Gregorian time:
outputs
PS: your code sets locale twice.