我有以下字符串作为输入:
BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
SUMMARY;CHARSET=utf-8:hello
LOCATION;CHARSET=utf-8:loc
DTSTART:20120704T053000Z
DTEND:20120704T073000Z
END:VEVENT
END:VCALENDAR
我面临的问题是我正在使用的日期部分:
NSRange end = [Result rangeOfString:@"T"];
NSString *DT=[NSString stringWithFormat:@"%@%@",[Result substringToIndex:end.location],[Result substringFromIndex:end.location+1]];
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone defaultTimeZone]];
[formatter setDateFormat:@"yyyyMMddHHmmssZ"];
NSDate *dt=[formatter dateFromString:DT];
myEvent.startDate=dt;
[formatter release];
dt
返回这里是 NULL ,我做错了什么?
更新代码:我更新代码如下,现在可以获取日期但需要问,我需要设置语言环境或时区吗?
[NSTimeZone resetSystemTimeZone];
NSLocale *enUSPOSIXLocale;
NSDateFormatter *sRFC3339DateFormatter = [[NSDateFormatter alloc] init];
if( [Result characterAtIndex:[Result length]-2] == 'Z')
{
NSLog(@"in 1");
[sRFC3339DateFormatter setDateFormat:@"yyyyMMdd'T'HHmmss"];
}
else
{
NSLog(@"in 2");
[sRFC3339DateFormatter setDateFormat:@"yyyyMMdd'T'HHmmssZ"];
}
NSDate *date = [sRFC3339DateFormatter dateFromString:[Result substringToIndex:[Result length]-2]];
myEvent.startDate=date;