NSString到NSDate转换返回null

han*_*Dev -1 objective-c nsdate nsstring ios

NSStringNSDate转换的回报null.

原始字符串输出" 2013-07-16T18:42:56 + 02:00 ",但当我尝试转换为NSDate它时返回"null"

这是我的代码:

// original string
NSString *str = [timeStamp objectAtIndex:indexPath.row];

// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

//[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"]; // updated from Stavash's post below. Still null is outputted for the date though
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:str];

cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",dte ];
Run Code Online (Sandbox Code Playgroud)

Sta*_*ash 6

为什么你将+11:00硬编码为格式?我认为这对你有用(在iOS6.0 +上):

[dateFormat setDateFormat:@"yyyy-MM-dd\'T\'HH:mm:ssZZZZZ"];
Run Code Online (Sandbox Code Playgroud)

对于iOS5.0 +使用:

[dateFormat setDateFormat:@"yyyy-MM-dd\'T\'HH:mm:ssZZZ"];
Run Code Online (Sandbox Code Playgroud)

更新

请参阅下面的Rob的评论 - 在iOS5.0 +上,日期格式仅适用于没有冒号的时区.