NSDateFormatter 踩坑一记

0 条回复
13 次浏览

err code:

复制
    NSString *endTime = [[NSString alloc] initWithData:[NSString hexStringToBytes:[dkInfo substringWithRange:NSMakeRange(44, 32)]] encoding:NSUTF8StringEncoding]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd'T'HHmmss'Z'"];
    NSDate *endDate = [dateFormatter dateFromString:endTime]; 

现象:
ios 用户设置时间显示为十二进制的时候这个 dateFromString 会返回 nil,二十四进制正常。

原因:
NSDateFormatter 底层依赖 ICU(International Components for Unicode)库。这个库强制关联了系统“设置->通用->日期与时间->24 小时制”的开关。
在我这里没有设置 locale 的情况下它识别到>12h 且没 am 和 pm 标识就会空指针。
怎么解决呢?描述里有这一段:

复制
    When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string. For most fixed formats, you should also set the locale property to a POSIX locale ("en_US_POSIX"), and set the timeZone property to UTC.

加上 dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];就行了 或者直接切换为 ISO8601DateFormatter 来处理格式化的问题。

感觉有点反直觉。。。 总之:参考代码别看太老的文章,用到的方法最好去官网查一下,新写的部分一定要打日志。勿以逻辑小而粗判不 log。sobbing

发表一个评论

R保持