banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

iOS开发之NSDateFormatter的NSLocale使用en_US_POSIX还是en_US

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 文档中给出了答案.

在大多数情况下,最好选择 "en_US_POSIX" 作为区域设置。

因为

"en_US_POSIX" 在时间上也是不变的(如果美国在将来某个时候更改日期格式的方式,"en_US" 将会反映出新的行为,但 "en_US_POSIX" 不会),并且在不同设备上也是一样的("en_US_POSIX" 在 iPhone OS 上与在 Mac OS X 上以及其他平台上的表现相同)。

参考链接 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

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。