NSDateFormatter仅适用于模拟器?

时间:2010-04-09 15:04:00

标签: iphone nsdateformatter

我简单地尝试重新格式化日期:

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 NSDate *date = [[NSData alloc] init];
 //Mon, 05 Apr 2010 14:27:48 -0700
 [formatter setDateFormat:@"E, dd MMM yyyy HH:mm:ss Z"];
 date = [formatter dateFromString:self.currentItemValue];        

 [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
 self.currentItem.pubDate = [formatter stringFromDate:date];
 [formatter release];

它适用于模拟器,但在设备上我只得到(null)。

感谢您的帮助!

更多测试:

NSString *string = @"Fri, 12 Dec 2008 18:45:15 -0800";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:string];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
//here i got (null) on the device but it works on simulator...
NSLog(@"date %@", [dateFormatter stringFromDate:date]);

1 个答案:

答案 0 :(得分:6)

DyingCactus是对的!

[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]];

解决了我的问题....谢谢!!!!