为什么我的NSDate Formatter无法正确转换并在设备上返回null? [英] Why my NSDate Formatter can't convert correctly and returns null on device?

查看:115
本文介绍了为什么我的NSDate Formatter无法正确转换并在设备上返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSDate格式化程序,应该从字符串创建一个NSDate并应该返回它格式化。问题是从String转换为NSDate的结果是错误的,更大的问题是在iPhone OS 3.1.2上它返回null,而在模拟器中它返回错误的日期。

I have an NSDate Formatter that should create an NSDate from string and should return it formated. The problem is that the result of converting from String to NSDate is wrong, the bigger problem is that on iPhone OS 3.1.2 it returns null while in the simulator it returns a wrong date.

//start formating date
NSMutableString *rawNewsDate = [NSMutableString stringWithString:@"Wed, 3 Feb 2010 14:47:11 CET"];
[rawNewsDate replaceOccurrencesOfString:@" CET" withString:@"" options:0 range:NSMakeRange(0, [rawNewsDate length])];

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE, d MMM YYYY HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:rawNewsDate]; 
[dateFormat setDateFormat:@"d.M.YYYY | HH:mm"];
NSString *formatedDate = [dateFormat stringFromDate:date];  
NSLog(@"Formated Date %@", formatedDate);
[dateFormat release];

在控制台的模拟器上,运行代码后,从字符串转换的NSDate是2009- 12-23 14:47:11 +0200和格式化的23.12.2009 | 14:47。

On the simulator in the console, after running the code, the NSDate converted from the string is 2009-12-23 14:47:11 +0200 and the formated one 23.12.2009 | 14:47 .

在控制台中的设备上,从字符串转换的NSDate为空,格式化也为空。

On the device in the console the NSDate converted from the string is null and the formated also null.

结果应该是03.02.2010 | 14:47。

The result should be 03.02.2010 | 14:47.

任何指针?
谢谢

Any pointers? Thanks

推荐答案

在格式字符串中,将YYYY更改为yyyy(小写)。看看这是否修复了模拟器结果。

In the format strings, change YYYY to yyyy (lowercase). See if this fixes the simulator result.

如果设备上仍然存在错误,是否设置为美国地区?如果没有,请尝试在格式化程序上将setLocale设置为en_US。

If it's still wrong on the device, is it set to US region? If not, try doing a setLocale on the formatter to en_US.

以下是日期格式模式的参考:

http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns

Here is a reference for date format patterns:
http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns

这篇关于为什么我的NSDate Formatter无法正确转换并在设备上返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆