Objective-c 根据某一个时区的时间获取特定时区的时间
//获取默认时区的时间字符串
NSDateFormatter*formatter=[[NSDateFormatteralloc]init];
[formattersetDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSString*nowStr=[formatterstringFromDate:[NSDatedate]];
NSLog(@"nowtimewithoutsettingTimeZone\nDefaultTimeZone:%@,LocalTime%@",[formattertimeZone],nowStr);
NSDate*nowGMT=[formatterdateFromString:nowStr];
NSLog(@"当地时间->标准时间%@",nowGMT);
NSLog(@"标准时间->当地时间%@",[formatterstringFromDate:nowGMT]);
//获取特定时区的时间字符串
[formattersetTimeZone:[NSTimeZonetimeZoneWithName:@"America/Adak"]];
NSString*AmericanNow=[formatterstringFromDate:[NSDatedate]];
NSLog(@"nowtimewithcertaintimezone:%@LocalTime%@",[formattertimeZone],AmericanNow);
NSDate*AmericanGMT=[formatterdateFromString:AmericanNow];
NSLog(@"特定时区的时间->标准时间%@",AmericanGMT);
NSLog(@"标准时间->特定时区的时间%@",[formatterstringFromDate:AmericanGMT]);
[formatterrelease];
/*将系统默认时间转换称某个特别时区的时间[color=darkred][/color][size=x-large][/size]
Step1:获取系统默认时间的时间字符串
*/
NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];
[dateFormattersetDateFormat:@"yyyy-MM-ddHH:mm:SS"];
NSString*defaultNowStr=[dateFormatterstringFromDate:[NSDatedate]];
NSLog(@"获取系统默认时间的时间字符串%@",defaultNowStr);
/*
Step2:获取标准时间
*/
NSDate*dateGMT=[dateFormatterdateFromString:defaultNowStr];
NSLog(@"获取标准时间%@",dateGMT);
/*将标准时间转换称特定时区的时间
*/
[dateFormattersetTimeZone:[NSTimeZonetimeZoneWithName:@"America/Adak"]];
NSString*localDateStr=[dateFormatterstringFromDate:dateGMT];
NSLog(@"将标准时间转换称特定时区的时间%@",localDateStr);
[dateFormatterrelease];
//将某个时区的特定时间转化称另一个时区的对应时间
/*
该例子将时区Asia/Tokyo的2011-12-3016:45:00转化为
时区America/Adak的相应时间
*/
NSDateFormatter*dateFormatter2=[[NSDateFormatteralloc]init];
[dateFormatter2setDateFormat:@"yyyy-MM-ddHH:mm:SS"];
NSString*fromTimeZone=[[NSStringalloc]initWithString:@"Asia/Tokyo"];
NSString*date_fromTimeZone=[[NSStringalloc]initWithString:@"2011-12-3016:45:00"];
[dateFormatter2setTimeZone:[NSTimeZonetimeZoneWithName:fromTimeZone]];
NSDate*dateOfGMT=[dateFormatter2dateFromString:date_fromTimeZone];
[fromTimeZonerelease];
[date_fromTimeZonerelease];
NSString*toTimeZone=[[NSStringalloc]initWithString:@"America/Adak"];
[dateFormatter2setTimeZone:[NSTimeZonetimeZoneWithName:toTimeZone]];
NSString*dateStrDst=[dateFormatter2stringFromDate:dateOfGMT];
NSLog(@"dateStrDst%@",dateStrDst);
[toTimeZonerelease];
[dateFormatter2release];