我编写了转换当前时间 utc-gmt 或 gmt -utc 的函数。如果 msgArrivedDate 为空,这些函数就可以正常工作。如果不是(这意味着 msgArrivedDate 来自未转换的休息服务。
JSON解析部分:
NSArray *messageSentTime = [[args valueForKey:@"messageSendDate"] objectAtIndex:0];
for(int i=0 ;i< [messageSentTime count]; i++)
{
//[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]];
NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]);
功能部分:
-(id)timeZoneFormatter:(NSString *)formatType : (NSString *)msgArrivedDate
{
NSDate *date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
if([msgArrivedDate length] > 0)
{
date = [dateFormatter dateFromString:msgArrivedDate];
} else {
date = [NSDate date];
}
if([formatType isEqualToString:@"UTC"])
{
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
}
if([formatType isEqualToString:@"GMT"])
{
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
}
NSString *dateString = [dateFormatter stringFromDate:date];
return dateString;
}
ReST 以 UTC 格式返回这些值:
"2013-09-24 15:03:17", "2013-09-25 12:09:22", "2013-09-25 13:07:45", "2013-09-25 13:08:19", "2013-09-25 14:22:38"
当我调用该函数时,(NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]))
返回:
messageSentTime = (
"2013-09-24 15:03:17",
"2013-09-25 12:09:22",
"2013-09-25 13:07:45",
"2013-09-25 13:08:19",
"2013-09-25 14:22:38" )
我想我只是在这里遗漏了小问题:(还找不到...