前言

	@interface NSTimeZone : NSObject <NSCopying, NSSecureCoding>
  • NSTimeZone 表示时区信息。

1、NSTimeZone 时区的创建

	NSTimeZone *zone1 = [[NSTimeZone alloc] init];

	// 根据时区名称创建
NSTimeZone *zone2 = [[NSTimeZone alloc] initWithName:@"America/Chicago"];
NSTimeZone *zone3 = [NSTimeZone timeZoneWithName:@"America/Chicago"]; // 根据时区缩写创建
NSTimeZone *zone4 = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; // 根据零时区的秒数偏移量创建
NSTimeZone *zone5 = [NSTimeZone timeZoneForSecondsFromGMT:28800];

2、NSTimeZone 时区的设置

	// 设置默认的时区
[NSTimeZone setDefaultTimeZone:[[NSTimeZone alloc] initWithName:@"America/Chicago"]]; NSTimeZone *systemZone = [NSTimeZone systemTimeZone]; // 本地时区可以被修改,而系统时区不能修改。
NSTimeZone *localZone = [NSTimeZone localTimeZone]; NSTimeZone *defaultZone = [NSTimeZone defaultTimeZone]; // NSCalendar 设置时区
/*
设置时区,设置为 GMT+8,即北京时间(+8)
*/
NSCalendar *calendar = [NSCalendar currentCalendar]; [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CET"]];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:+28800]]; // NSDateFormatter 设置时区
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // 设置时区,设置为 GMT
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

3、NSTimeZone 时区的获取

	// 获取系统时区
NSTimeZone *zone1 = [NSTimeZone systemTimeZone]; // 获取本地时区
NSTimeZone *zone2 = [NSTimeZone localTimeZone]; // 获取默认时区
NSTimeZone *zone3 = [NSTimeZone defaultTimeZone];

4、获取时区的名称

	// 获取所有已知的时区名称
NSArray *zoneNames = [NSTimeZone knownTimeZoneNames]; NSTimeZone *zone = [NSTimeZone localTimeZone]; // 获取指定时区的名称
NSString *strZoneName = [zone name];

5、获取时区的缩写

	// 获取所有已知的时区缩写
NSDictionary *zoneAbbreviations = [NSTimeZone abbreviationDictionary]; NSTimeZone *zone = [NSTimeZone localTimeZone]; // 获取指定时区的缩写
NSString *zoneAbbreviation1 = [zone abbreviation]; // 获取指定时间所在时区名称缩写
NSString *zoneAbbreviation2 = [zone abbreviationForDate:[NSDate date]];

6、获取与零时区的间隔秒数

	NSTimeZone *zone = [NSTimeZone localTimeZone];

	// 获取当前时区与零时区的间隔秒数
NSInteger seconds1 = [zone secondsFromGMT]; // 获取指定时间所在时区与零时区的间隔秒数
NSInteger seconds2 = [zone secondsFromGMTForDate:[NSDate date]];

7、时区差值转换

	// 得到当前时间(世界标准时间 UTC/GMT)
NSDate *date = [NSDate date]; // 设置系统时区为本地时区
NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 计算本地时区与 GMT 时区的时间差
NSInteger interval = [zone secondsFromGMT]; // 在 GMT 时间基础上追加时间差值,得到本地时间
date = [date dateByAddingTimeInterval:interval];

iOS - OC NSTimeZone 时区的更多相关文章

  1. iOS - Swift NSTimeZone 时区

    前言 public class NSTimeZone : NSObject, NSCopying, NSSecureCoding NSTimeZone 表示时区信息. 1.NSTimeZone 时区的 ...

  2. NSDate|NSTimeZone|时区|日历

    NSDate,NSDateFormatter以及时区转换-开发者-51CTO博客 iOS 时区转换 东八区 - 简书 iOS时间的时区转换以及一些方法记录 - 简书 iOS - OC NSTimeZo ...

  3. iOS - OC NSCalendar 日历

    前言 @interface NSCalendar : NSObject <NSCopying, NSSecureCoding> NSCalendar 对世界上现存的常用的历法进行了封装,既 ...

  4. iOS - OC 面向对象语法

    1.类 1)根类:因为类 NSObject 是层次结构的最顶层,因此称为根类. 可以将类称为子类(subclass)和父类(superclass),也可以将类称为子类和超类. 2)分类/类别(cate ...

  5. ios+oc面试题

    ios+oc面试题     浅复制和深复制的区别?//浅拷贝和深拷贝答案:浅层复制(copy):只复制指向对象的指针,而不复制引用对象本身.//通过对象的指针来访问这个对象深层复制(mutableCo ...

  6. iOS OC语言原生开发的IM模块--RChat

    iOS OC语言原生开发的IM模块,用于项目中需要原生开发IM的情况,具备发送文字.表情.语音.图片.视频等完整功能,包含图片预览视频播放等功能,此项目将会长期更新如有问题可以提出,我的邮箱:fshm ...

  7. NSTimeZone时区

    前言 NSTimeZone 表示时区信息. 1.NSTimeZone 时区的创建 NSTimeZone *zone1 = [[NSTimeZone alloc] init]; // 根据时区名称创建 ...

  8. iOS - OC NSDate 时间

    前言 NSDate @interface NSDate : NSObject <NSCopying, NSSecureCoding> NSDate 用来表示公历的 GMT 时间(格林威治时 ...

  9. iOS - OC Foundation 框架

    前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...

随机推荐

  1. 转载WPF SDK研究 之 AppModel

    Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...

  2. MySQL Replication 优化和技巧、常见故障解决方法

    MySQL 主从同步错误(error)解决(转) sql_slave_skip_counter参数 附: 一些错误信息的处理,主从服务器上的命令,及状态信息. 在从服务器上使用show slave s ...

  3. 用excel2010 制作复合图表

    用excel2010制作双轴柱线复合图表就是要用excel2010做一个这样的图表:

  4. iOS开发必备HUD(透明指示层)

    iOS开发必备HUD(透明指示层) 字数421 阅读2123 评论1 喜欢51 1.MBProgressHUD GitHub地址:https://github.com/jdg/MBProgressHU ...

  5. thinkphp和uploadfiy

    上传页面 用的是bootstrap <div class="col-sm-6"> <div style="width: 200px; height: 1 ...

  6. javaWEB中的HttpServlet(企业开发使用)

    HttpServlet: 1). 是一个 Servlet, 继承自 GenericServlet. 针对于 HTTP 协议所定制. 2). 在 service() 方法中直接把 ServletReuq ...

  7. ROADS

    ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11977 Accepted: 4429 Description N ...

  8. Software caused connection abort: socket write error

    Exception in thread "main" java.net.SocketException: Software caused connection abort: soc ...

  9. Java中的内部类与匿名内部类总结

    内部类 内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) ...

  10. 2016年10月23日 星期日 --出埃及记 Exodus 19:7

    2016年10月23日 星期日 --出埃及记 Exodus 19:7 So Moses went back and summoned the elders of the people and set ...