NSTimeZone表示时区信息。 有下面几种初始化方法

1. + (id)timeZoneWithName:(NSString *)aTimeZoneName / – (id)initWithName:(NSString *)aName

根据时区名称初始化可以调用NSTimeZone的类方法 + (NSArray *)knownTimeZoneNames返回所有已知的时区名称

NSTimeZone *zone = [[NSTimeZone alloc] initWithName:@”America/Chicago“];

//NSTimeZone *zone = [NSTimeZone timeZoneWithName:@”America/Chicago“];

NSLog(@”%@”,zone);

打印出:America/Chicago (CST) offset -21600

2. + (id)timeZoneWithAbbreviation:(NSString *)abbreviation

根据时区缩写初始化例如:EST(美国东部标准时间)、HKT(香港标准时间)

NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@”HKT”];

NSLog(@”%@”,zone);

打印出:Asia/Hong_Kong (HKT) offset 28800

3. + (NSTimeZone *)systemTimeZone

返回系统时区

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSLog(@”%@”,zone);

假如时区是上海,打印出的时区信息将会是:Asia/Shanghai (CST (China)) offset 28800,28800代表相对于GMT时间偏移的秒数,即8个小时。(8*60*60)

4. + (NSTimeZone *)localTimeZone

返回本地时区,与systemTimeZone的区别在于:本地时区可以修改,而系统时区不能修改

[NSTimeZone setDefaultTimeZone:[[NSTimeZone alloc] initWithName:@”America/Chicago”]];

NSTimeZone *systemZone = [NSTimeZone systemTimeZone];

NSTimeZone *localZone = [NSTimeZone localTimeZone];

NSLog(@”%@”,systemZone);

NSLog(@”%@”,localZone);

打印出的系统时区仍然是:Asia/Shanghai (CST (China)) offset 28800;而本地时区经过修改后,变成了:Local Time Zone (America/Chicago (CST) offset -21600)

5. + (id)timeZoneForSecondsFromGMT:(NSInteger)seconds

根据零时区的秒数偏移返回一个新时区对象

NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:28800];

NSLog(@”%@”,zone);

打印出:GMT+0800 (GMT+08:00) offset 28800

NSTimeZone常用对象方法与类方法

1. + (NSArray *)knownTimeZoneNames

数组的形式返回所有已知的时区名称

NSArray *zoneArray = [NSTimeZone knownTimeZoneNames];

for(NSString *str in zoneArray)

{

    NSLog(@”%@”,str);

}

2. – (NSString *)name / – (NSString *)abbreviation

返回时区对象的名称或缩写

NSTimeZone *zone = [NSTimeZone localTimeZone];

NSString *strZoneName = [zone name];

NSString *strZoneAbbreviation = [zone abbreviation];

NSLog(@”name is %@”,strZoneName);

NSLog(@”abbreviation is %@”,strZoneAbbreviation);

name is Asia/Hong_Kong

abbreviation is HKT

3. – (NSInteger)secondsFromGMT

得到当前时区与零时区的间隔秒数

NSTimeZone *zone = [NSTimeZone localTimeZone];

int seconds = [zone secondsFromGMT];

NSLog(@”%i”,seconds);

原文地址:https://blog.csdn.net/feifei_iong/article/details/122600676

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_25544.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注