NSDateFormatter对于@& quot; dd-MM-yy& quot;返回nil。在iOS 3.0中 [英] NSDateFormatter returns nil for @"dd-MM-yy" in iOS 3.0

查看:100
本文介绍了NSDateFormatter对于@& quot; dd-MM-yy& quot;返回nil。在iOS 3.0中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这部分代码:

NSDate *date =nil;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"dd-MM-yy"];
date = [dateFormatter dateFromString:inString];
[dateFormatter release];

它的工作原理非常好,正如iOS 4.0中预期的那样。但是相同的代码在3.0中没有。

It works perfectly fine, as expected in iOS 4.0. But the same code doesnt in 3.0.

我得到的字符串就像 12-Nov-10,它包含在inString指针中。

The string which I am getting, is like "12-Nov-10" and this is contained in inString pointer.

如果本机OS是3.0或3.1,则日期格式器将返回nil。由于某些原因,我需要坚持相同的日期格式。还有其他人遇到过这个问题吗?有解决此问题的建议吗?

The date formatter returns nil if the native OS is 3.0 or 3.1. For some reasons I need to stick to the same date format. Has anyone else faced this problem? Any suggestions to resolve this issue?

谢谢

Raj

编辑:
在遵循Harkonian和Q& A讨论所指出的建议之后,正确的代码:

The proper code, after following suggestions pointed out by Harkonian and the Q&A discussions:

NSDate *date =nil;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"dd-MMM-yy"];

NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[enUSPOSIXLocale release];

date = [dateFormatter dateFromString:inString];
[dateFormatter release];


推荐答案

如果您要使用用户可见的日期,您应该避免设置日期格式字符串,因为很难预测格式字符串在所有可能的用户配置中的表示方式。相反,您应该尝试限制自己设置日期和时间样式(通过-[NSDateFormatter setDateStyle:]和-[NSDateFormatter setTimeStyle:])。

If you're working with user-visible dates, you should avoid setting a date format string because it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should try and limit yourself to setting date and time styles (via -[NSDateFormatter setDateStyle:] and -[NSDateFormatter setTimeStyle:]).

,如果您使用固定格式的日期,则应首先将日期格式器的语言环境设置为适合您的固定格式的语言环境。在大多数情况下,最好的语言环境是 en_US_POSIX,该语言环境专门用于产生美式英语结果,而与用户和系统偏好设置无关。 en_US_POSIX的时间也不变(如果美国在将来的某个时候改变其日期格式的方式, en_US将更改以反映新的行为,但 en_US_POSIX不会改变)以及机器之间( en_US_POSIX在iPhone OS上的工作方式与在Mac OS X上以及在其他平台上的工作方式相同。)

On the other hand, if you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences. "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).

设置了 en_US_POSIX后,作为日期格式化程序的语言环境,您可以设置日期格式化字符串,然后日期格式化程序将对所有用户一致。

Once you've set "en_US_POSIX" as the locale of the date formatter, you can then set the date format string and the date formatter will behave consistently for all users.

上面的信息以及更多信息都可以找到在Apple的技术问题与解答QA1480

The above info and more can be found in Apple's Technical Q&A QA1480

这是我应用中的代码段,实现了上述建议:

Here's a snippet of code from my app which implements the above recommendation :

static NSDateFormatter* dateFormatter = nil;
if (!dateFormatter) {
   dateFormatter = [[NSDateFormatter alloc] init];

   NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] 
      initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
   assert(enUSPOSIXLocale != nil);
   [dateFormatter setLocale:enUSPOSIXLocale];
   [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
   dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss +0000";
}

这篇关于NSDateFormatter对于@& quot; dd-MM-yy& quot;返回nil。在iOS 3.0中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆