1

在 Apple Docs 中,他们说 NSDateFormatter 使用 Unicode 作为规范。我已经阅读了规范,但我在解释这个日期时遇到了麻烦:

NSDateFormatter *frm = [[NSDateFormatter alloc] init];
[frm setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSLog(@"Tue Oct 13 09:24:15 +0000 2009 becomes %@", [frm dateFromString:@"Tue Oct 13 09:24:15 +0000 2009"]);

这段代码打印出来:

2009-10-13 11:37:40.334 test2[1704:20b] Tue Oct 13 09:24:15 +0000 2009 becomes (null)
4

1 回答 1

2

对不起各位,已经想通了。NSDateFormatter 默认使用当前系统语言环境。在上面的代码示例中,仅当您将系统区域设置为美国以外的其他设置时,它才会失败。要修复它,就在将区域设置设置为日期字符串使用的区域之前,如下所示:

[frm setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
于 2009-10-13T10:07:04.263 回答