日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

NSTimeZone时区

發(fā)布時間:2023/12/13 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 NSTimeZone时区 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

NSTimeZone 表示時區(qū)信息。

1、NSTimeZone 時區(qū)的創(chuàng)建

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

// 根據(jù)時區(qū)名稱創(chuàng)建
NSTimeZone *zone2 = [[NSTimeZone alloc] initWithName:@"America/Chicago"];
NSTimeZone *zone3 = [NSTimeZone timeZoneWithName:@"America/Chicago"];

// 根據(jù)時區(qū)縮寫創(chuàng)建
NSTimeZone *zone4 = [NSTimeZone timeZoneWithAbbreviation:@"EST"];

// 根據(jù)零時區(qū)的秒數(shù)偏移量創(chuàng)建
NSTimeZone *zone5 = [NSTimeZone timeZoneForSecondsFromGMT:28800];

2、NSTimeZone 時區(qū)的設(shè)置

// 設(shè)置默認(rèn)的時區(qū)
[NSTimeZone setDefaultTimeZone:[[NSTimeZone alloc] initWithName:@"America/Chicago"]];

NSTimeZone *systemZone = [NSTimeZone systemTimeZone];

// 本地時區(qū)可以被修改,而系統(tǒng)時區(qū)不能修改。
NSTimeZone *localZone = [NSTimeZone localTimeZone];

NSTimeZone *defaultZone = [NSTimeZone defaultTimeZone];

// NSCalendar 設(shè)置時區(qū)
/*
設(shè)置時區(qū),設(shè)置為 GMT+8,即北京時間(+8)
*/
NSCalendar *calendar = [NSCalendar currentCalendar];

[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CET"]];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:+28800]];

// NSDateFormatter 設(shè)置時區(qū)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

// 設(shè)置時區(qū),設(shè)置為 GMT
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

3、NSTimeZone 時區(qū)的獲取

// 獲取系統(tǒng)時區(qū)
NSTimeZone *zone1 = [NSTimeZone systemTimeZone];

// 獲取本地時區(qū)
NSTimeZone *zone2 = [NSTimeZone localTimeZone];

// 獲取默認(rèn)時區(qū)
NSTimeZone *zone3 = [NSTimeZone defaultTimeZone];

4、獲取時區(qū)的名稱

// 獲取所有已知的時區(qū)名稱
NSArray *zoneNames = [NSTimeZone knownTimeZoneNames];

NSTimeZone *zone = [NSTimeZone localTimeZone];

// 獲取指定時區(qū)的名稱
NSString *strZoneName = [zone name];

5、獲取時區(qū)的縮寫

// 獲取所有已知的時區(qū)縮寫
NSDictionary *zoneAbbreviations = [NSTimeZone abbreviationDictionary];

NSTimeZone *zone = [NSTimeZone localTimeZone];

// 獲取指定時區(qū)的縮寫
NSString *zoneAbbreviation1 = [zone abbreviation];

// 獲取指定時間所在時區(qū)名稱縮寫
NSString *zoneAbbreviation2 = [zone abbreviationForDate:[NSDate date]];

6、獲取與零時區(qū)的間隔秒數(shù)

NSTimeZone *zone = [NSTimeZone localTimeZone];

// 獲取當(dāng)前時區(qū)與零時區(qū)的間隔秒數(shù)
NSInteger seconds1 = [zone secondsFromGMT];

// 獲取指定時間所在時區(qū)與零時區(qū)的間隔秒數(shù)
NSInteger seconds2 = [zone secondsFromGMTForDate:[NSDate date]];

7、時區(qū)差值轉(zhuǎn)換

// 得到當(dāng)前時間(世界標(biāo)準(zhǔn)時間 UTC/GMT)
NSDate *date = [NSDate date];

// 設(shè)置系統(tǒng)時區(qū)為本地時區(qū)
NSTimeZone *zone = [NSTimeZone systemTimeZone];

// 計算本地時區(qū)與 GMT 時區(qū)的時間差
NSInteger interval = [zone secondsFromGMT];

// 在 GMT 時間基礎(chǔ)上追加時間差值,得到本地時間
date = [date dateByAddingTimeInterval:interval];

總結(jié)

以上是生活随笔為你收集整理的NSTimeZone时区的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。