当用户不使用公历作为 iPhone 的默认日历时,我在使用 NSDateFormatter 时遇到问题。
NSString *testString = @"2011-01-14";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDate *eventDate = [dateFormat dateFromString:testString];
[dateFormat setDateFormat:@"yyyy-MM-dd(EEE)"];
NSString *dateAndLocString = [dateFormat stringFromDate:eventDate];
[dateFormat release];
在使用公历的设备上 dateAndLocString 将等于 2011-01-13(Thu)
但是当用户使用日本日历时 dateAndLocString 将等于 2011-01-13(Wed),这是错误的。
有人知道我可能做错了什么吗?
我找到了一种让它正常工作的方法,如果有人有更好的建议请告诉我。我们必须使用以下代码行在 NSDateFormatter 上设置本地首选用户。
[dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]] autorelease]];
这将为 NSDateFormatter 提供正确的用户区域设置。有些人可能会想使用
[dateFormat setLocale:[NSLocale currentLocale]];
但这不会起作用[NSLocale currentLocale]
在具有日语日历的系统上,即使用户语言设置为日语,也会返回 en_US@calendar=japanese。
我不知道为什么 NSDateFormatter 不能在非公历上自动工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)