3

我编写了转换当前时间 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" )

我想我只是在这里遗漏了小问题:(还找不到...

4

1 回答 1

15

改变这个:

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

对此:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

编辑:此方法将处理将日期字符串从 UTC 转换为 GMT 以及从 GMT 转换为 UTC:

- (id)translateDate:(NSString *)msgArrivedDate
               from:(NSString *)fromTimeZone
                 to:(NSString *)toTimeZone {
    NSDate *date = nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    if([fromTimeZone isEqualToString:@"UTC"]) {
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    }

    if([fromTimeZone isEqualToString:@"GMT"]) {
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    }

    if([msgArrivedDate length] > 0) {
        date = [dateFormatter dateFromString:msgArrivedDate];
    } else {
        date = [NSDate date];
    }

    if([toTimeZone isEqualToString:@"UTC"]) {
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    }

    if([toTimeZone isEqualToString:@"GMT"]) {
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    }

    NSString *dateString = [dateFormatter stringFromDate:date];
    return dateString;
}

将“2013-09-25 14:22:38”从 UTC 转换为 GMT(即本地时间,在您使用 GMT 时)将导致2013-09-25 15:22:38,同样,从 GMT 转换“2013-09-25 14:22:38”到 UTC 将导致2013-09-25 13:22:38.

于 2013-09-26T10:59:11.133 回答