iOS SDK 提供的日期时间格式化对象需要使用到 NSLocale 对象来控制日期和时间的显示,en_US 为标准的格式往往使用的最多,但是习惯使用 Java 或 C# 的朋友会忽略掉 iOS SDK 提供的另一种格式 en_US_POSIX, 并且两种格式运行后的效果完全一样。
参考代码;
NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease];
[formatter1 setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
NSDateFormatter *formatter2 = [[[NSDateFormatter alloc] init] autorelease];
[formatter2 setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[formatter1 setDateFormat:@"yyyy/MM/dd HH:mm"];
[formatter2 setDateFormat:@"yyyy/MM/dd HH:mm"];
NSDate *date = [NSDate date];
NSLog(@"formatter1:%@",[formatter1 stringFromDate]);
NSLog(@"formatter2:%@",[formatter2 stringFromDate]);
到底使用 en_US_POSIX 还是 en_US, Apple 在 SDK 文档中给出了答案.
In most cases the best locale to choose is "en_US_POSIX"
因为
"en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iPhone OS as it does on Mac OS X, and as it it does on other platforms).
参考链接 http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature http://developer.apple.com/library/ios/#qa/qa1480/_index.html