Nsdateformatter does not work with Japanese calendar

I see a problem with NSDateFormatter on iOS when Settings / General / International / Calendar is installed both Japanese and Buddhist on both the simulator and the real device. Year not analyzed 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 
+4
source share
1 answer

It works as it should. I took your code and added it to my project, and then set up 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 conclusion:

 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:

Everything works perfectly. In the Buddhist calendar, the year is 2555. When you give the Gregorian date and ask to format using the Gregorian calendar, he reads it correctly, then converts it to a Buddhist date, and when you print it, the date is again 2555. What you expect.

EDIT: just to click a point, NSDate will always be the same what changes are its representation. So I set the calendar to Buddhism 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]]); 

exits

 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 the locale twice.

+4
source

All Articles