1
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

 dateFormatter.dateFormat       = @"EEE, dd MM yyyy HH:mm:ss VVVV";

 NSDateFormatter *df1 = [[NSDateFormatter alloc] init];

 [df1 setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];

 NSString *dateString           = @"Mon, 29 Apr 2013 01:00:49 +0000";

 NSDate *date1= [dateFormatter dateFromString:dateString];

 NSString *localDateString      = [df1 stringFromDate:date1];

 NSLog(@"localDateString=%@", localDateString); 

当我使用常量 dateString 值时,它会给出正确的输出。当我从 xml paring value 中给出输入时,dateString 返回 nil 值

4

3 回答 3

1

也许您在字符串的开头或结尾有一些额外的空格。使用比较两个字符串的长度

dateString.length;

otherDateString.length;

您还可以通过使用比较并查看这些字符串是否相等

[dateString isEqualToString:otherDateString];

这可能会返回错误。

于 2013-04-29T09:38:03.317 回答
1

两个可能的问题:

  1. VVVV不是有效的字段符号。你可能想要Z. 见http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

  2. 确保您的格式化程序设置为英语(非默认)语言环境,否则类似MonApr无法识别的模式。

于 2013-04-29T09:42:47.917 回答
0

您获得的日期字符串可能不是正确的字符串格式,因此首先您必须检查它。

以这种方式尝试:

NSString *dateString  = [NSString stringWithFormat:@"%@",yourXMLValue];

希望这会有所帮助。

于 2013-04-29T09:30:25.043 回答