iphone中NSDateFormatter模拟器可以正常显示,但在真机上不行,转换后的NSString为NULL,部分代码如下:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MMM dd HH:mm:ss zzzz yyyy"];
NSDate *date = [formatter dateFromString:createTime];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
NSString *dateStr = [formatter stringFromDate:date];
NSLog(@"%@",dateStr); //模拟器上正常,真机打出来为Null
createTimeLbl.text = dateStr;
[bgScrollVw addSubview:createTimeLbl];
[formatter release];
[createTimeLbl release];
找了半天,终于找到原因:
在真机运行需要设置Local,添加下面一段代码就OK:
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
[formatter setLocale:locale];