Pau*_*len 1 iphone objective-c ios
我正在尝试将NSString转换为NSDate.如果iPhone区域设置为英语(美国),它可以很好地完成,但是当我将它设置为瑞典语时,它不会.
我的代码:
[...]
// Get the date from the post
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZ"];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSDate *dateFromString = [[[NSDate alloc] init] retain];
dateFromString = [dateFormatter dateFromString:[[stories objectAtIndex:storyIndex] objectForKey: @"date"]];
NSLog(@"String: %@", [[stories objectAtIndex:storyIndex] objectForKey: @"date"]);
NSLog(@"date From string: %@", dateFromString);
// Set date string
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:MM"];
NSString *stringFromDate = [formatter stringFromDate:dateFromString];
stringFromDate = [stringFromDate stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSLog(@"StringDate: %@", [dateFormatter stringFromDate:[[NSDate alloc] init]]);
[...]
Run Code Online (Sandbox Code Playgroud)
记录结果:
[...]
2009-11-27 16:13:22.804 sportal.se[755:4803] String: Fri, 27 Nov 2009 12:56:13 +0100
2009-11-27 16:13:22.812 sportal.se[755:4803] date From string: (null)
2009-11-27 16:13:22.819 sportal.se[755:4803] StringDate: fre, 27 nov 2009 16:13:22 +0100
[...]
Run Code Online (Sandbox Code Playgroud)
这里的问题是它预计"fre,2009年11月27日...... + 0100",但它会得到"2009年11月27日星期五...... +0100".我怎样才能解决这个问题?
NSDateFormatter有房产locale.尝试:
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]
autorelease];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6616 次 |
| 最近记录: |