ios8 时区解析已更改?
ios8 Timezone parsing changed?
时区解析在 iOS8 中是否发生了变化?
我尝试解析日期“2014-09-03 12:20:38.000 +0200”
使用此代码并得到 nil:
-(NSDateFormatter*) dateAndTimeFormatter{
if(!_dateAndTimeFormatter){
_dateAndTimeFormatter = [[NSDateFormatter alloc]init];
[_dateAndTimeFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS ZZZZZ"];
_dateAndTimeFormatter.dateStyle = NSDateFormatterShortStyle;
_dateAndTimeFormatter.timeStyle = NSDateFormatterShortStyle;
}
return _dateAndTimeFormatter;
}
或者
[_dateAndTimeFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS Z"];
也不行。
我要尝试的第一件事是在 XCode 中重置您的时间设置:设置 -> 常规 -> 日期时间 -> 时间格式(设置为 24 小时或重置选择)。
如果这不起作用,here 是覆盖区域设置的指南,基本上是:
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
设置 dateStyle
或 timeStyle
将覆盖您设置的任何自定义 dateFormat
。您的日期字符串在任何方面都不符合 NSDateFormatterShortStyle
,这就是为什么您在尝试将字符串转换为日期时得到 nil 的原因。
此外,根据Unicode Technical Standard #35 revision 31,"ZZZZZ"的时区说明符对应格式为“-08:00”或“-07:52:58”的日期字符串,而不是“+” 0200" 就像你在你的例子中那样。匹配“+0200”的说明符之一是具有一到三个大写 "Z" 个字符的说明符。
这应该适合你:
- (NSDateFormatter *)dateAndTimeFormatter {
if (!_dateAndTimeFormatter) {
_dateAndTimeFormatter = [[NSDateFormatter alloc] init];
[_dateAndTimeFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS ZZZ"];
}
return _dateAndTimeFormatter;
}
另外请注意,您的代码在 iOS 8.
之前也不应该工作
时区解析在 iOS8 中是否发生了变化?
我尝试解析日期“2014-09-03 12:20:38.000 +0200”
使用此代码并得到 nil:
-(NSDateFormatter*) dateAndTimeFormatter{
if(!_dateAndTimeFormatter){
_dateAndTimeFormatter = [[NSDateFormatter alloc]init];
[_dateAndTimeFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS ZZZZZ"];
_dateAndTimeFormatter.dateStyle = NSDateFormatterShortStyle;
_dateAndTimeFormatter.timeStyle = NSDateFormatterShortStyle;
}
return _dateAndTimeFormatter;
}
或者
[_dateAndTimeFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS Z"];
也不行。
我要尝试的第一件事是在 XCode 中重置您的时间设置:设置 -> 常规 -> 日期时间 -> 时间格式(设置为 24 小时或重置选择)。
如果这不起作用,here 是覆盖区域设置的指南,基本上是:
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
设置 dateStyle
或 timeStyle
将覆盖您设置的任何自定义 dateFormat
。您的日期字符串在任何方面都不符合 NSDateFormatterShortStyle
,这就是为什么您在尝试将字符串转换为日期时得到 nil 的原因。
此外,根据Unicode Technical Standard #35 revision 31,"ZZZZZ"的时区说明符对应格式为“-08:00”或“-07:52:58”的日期字符串,而不是“+” 0200" 就像你在你的例子中那样。匹配“+0200”的说明符之一是具有一到三个大写 "Z" 个字符的说明符。
这应该适合你:
- (NSDateFormatter *)dateAndTimeFormatter {
if (!_dateAndTimeFormatter) {
_dateAndTimeFormatter = [[NSDateFormatter alloc] init];
[_dateAndTimeFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS ZZZ"];
}
return _dateAndTimeFormatter;
}
另外请注意,您的代码在 iOS 8.
之前也不应该工作