Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nsdateformatter not working with japanese calendar

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
like image 763
i_raqz Avatar asked Aug 27 '12 16:08

i_raqz


1 Answers

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.

NSLog(@"date=%@", [NSDate date]);
2012-08-27 18:42:10.201 Searcher[43537:f803] date=2555-08-27 22:42:10 +0000

    NSDateFormatter *formatter = [NSDateFormatter new];
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; // 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];
    //formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]];

Then the output:

NSLog(@"CorrectDate%@", @"Mon, 27 Aug 2012 16:33:14 GMT" );
2012-08-27 18:42:10.203 Searcher[43537:f803] CorrectDateMon, 27 Aug 2012 16:33:14 GMT

NSLog(@"Formatted date:%@",[formatter dateFromString:@"Mon, 27 Aug 2012 16:33:14 GMT"]);
2012-08-27 18:42:10.206 Searcher[43537:f803] Formatted date:2555-08-27 16:33:14 +0000

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:

NSLog(@"date=%@", [NSDate date]);
NSLog(@"Date using the Gregorian calendar: %@", [formatter stringFromDate:[NSDate date]]);

outputs

2012-08-28 07:13:10.658 Searcher[69194:f803] date=2555-08-28 11:13:10 +0000
2012-08-28 07:13:10.660 Searcher[69194:f803] Date using the Gregorian calendar: Tue, 28 Aug 2012 07:13:10 EDT

PS: your code sets locale twice.

like image 193
David H Avatar answered Oct 11 '22 04:10

David H