开发者

How retrieve current time of any country in my application?

I want to show the current time of any country. Suppose i want to find out current time of USA in Australia or i want to see current time of England in south Africa then how i retrieve current time of country? For that i was written an code but i does not worked,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];

NSDate *date = [NSDate date];

NSLocale *usLocale = [[NSLocale开发者_Go百科 alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];

The above code does not given me correct time.

Thanks


There is no such thing as time of USA or Australia. You have to use the correct timezone, and there are 4 on mainland USA, and 3 different timezones in australia.

To get the current time(string) of Melbourne, Berlin and Los Angeles you would use code like this:

 NSDate *now = [NSDate date];
 NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
 [df setTimeStyle:NSDateFormatterLongStyle];
 [df setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
 NSString *melbourneTime = [df stringFromDate:now];
 [df setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Berlin"]];
 NSString *berlinTime = [df stringFromDate:now];
 [df setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
 NSString *laTime = [df stringFromDate:now];
 NSLog(@"%@", melbourneTime);
 NSLog(@"%@", berlinTime);
 NSLog(@"%@", laTime);

The locale you've changed has influence on the format. For example the current time in melbourne in the en_US locale is November 18, 2010 9:14:28 PM GMT+11:00 but in de_DE it is 18. November 2010 21:14:28 GMT+11:00

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜