gpt4 book ai didi

iphone - nsdateformatter 不适用于日本日历

转载 作者:可可西里 更新时间:2023-11-01 04:42:23 25 4
gpt4 key购买 nike

当 Settings/General/International/Calendar 在模拟器和真实设备上都设置为日语或佛教时,我发现 iOS 上的 NSDateFormatter 出现问题。年份解析不正确

日期格式器

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

最佳答案

一切正常。我把你的代码添加到我的项目中,然后将模拟器设置为使用佛历。

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]];

然后输出:

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

分析:

一切正常。现在佛历是2555年。当您提供一个公历日期,并要求格式化程序使用公历时,它会正确读入,然后将其转换为佛教日期,当您打印出来时,日期又是 2555。正是您所期望的。

编辑:只是强调一下,NSDate 总是一样的,改变的是它的表示。所以我再次将日历设置为佛教日历,并使用您的格式化程序以公历时间获取当前时间:

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

输出

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:您的代码设置了两次语言环境。

关于iphone - nsdateformatter 不适用于日本日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12145997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com