我有以下字符串作为输入:VCAL日期解析问题
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;
@john谢谢你的答案我更新了我的代码,所以现在我需要设置区域设置或时区? – 2012-08-04 09:08:32
@HBastan:我对苹果API肯定不够了解,但我会*建议*将语言环境设置为某些“不变”语言环境(或者美国英语,如果没有这种情况),并设置时区到UTC。请注意,对于没有“Z”的值,该值表示一个* local *日期/时间 - 因此您应该使用vcal条目中指定的时区。是的,它变得棘手... – 2012-08-04 10:02:44