服务器返回的时间格式是NSString *DateString = @"2015-06-22T17:00:00+08:00";我自己转换的代码是:        NSString *DateString = @"2015-06-22T17:00:00+08:00";
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"];
        NSDate *currentDate = [dateFormatter dateFromString:DateString];
        NSLog(@"%@", currentDate);输出结果为:2015-06-22 09:00:00 +0000格式是我想要的,但是时间对不上了,这个事牵涉到时区的问题吗?请各位高手帮忙解答 

解决方案 »

  1.   

    //把GMT日期转换成UTC
    +(NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate
    {
        //设置源日期时区
        NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
        //设置转换后的目标日期时区
        NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
        //得到源日期与世界标准时间的偏移量
        NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
        //目标日期与本地时区的偏移量
        NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
        //得到时间偏移量的差值
        NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
        //转为现在时间
        NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
        return destinationDateNow;
    }
      

  2.   

    无视第一行非常感谢这位朋友的帮忙,你的方法对我很有用,但是还是有个不明白的地方NSDate *ddate = [self getNowDateFromatAnDate:currentDate];//您的转换方法
            NSLog(@"%@", ddate);//输出 2015-06-22 17:50:00 +0000        NSDateFormatter *dateFormat=[[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"HH:mm"];//我想截取出时间
            NSString *time = [dateFormat stringFromDate:ddate];
            NSLog(@"%@", time);//输出 01:50我用了你的方法得到了我想要的时间格式,但是我还需要取出具体的时间做UI显示
    为什么输出了第二天的1:50?