iOS 日期處理的相關類

GitHub: https://github.com/GardenerYun
Email: gardeneryun@foxmail.com
簡書博客地址: http://www.lxweimin.com/users/8489e70e237d/latest_articles
如有問題或建議請聯系我,我會馬上解決問題~ (? ??_??)?

索引

  1. NSDate - 表示一個絕對的時間點,表示公歷的GMT時間(格林威治時間),獨立于任何特定的歷法系統或時間區。
  2. NSLoale - 本地化信息,主要體現在"語言"和"區域格式"這兩個設置項。
  3. NSTimeZone - 時區信息。
  4. NSCalendar - 日歷類,提供日歷的信息、大量日期計算的接口。
  5. NSDateComponents - 封裝date對象關于年月日時秒分、周、季度等信息的類。
  6. NSDateFormatter - 由特定的格式,用來在date和String之間轉換。

簡書不支持目錄跳轉,有需要的請移步github

V1.1.0 - 16.07.19第三次更新 (公司項目開始開發新功能了... 更新只能放慢進度)
重寫NSDate及其方法描述,更全更詳細。

V1.0.1 - 16.06.24第二次更新 (就是這么快)
更新:日歷的demo、農歷處理方法 (NSDate+LunarCalendar.h .m)

日歷

V1.0.0 - 16.06.23第一次更新
項目中日期處理使用的越來越多,自己寫的方法不夠用了。開始使用DateTools(據說是最好用的日期處理庫),在研究源碼的時候,決定記錄下來。我只是重新造輪子。(會陸續更新簡單日歷的demo、農歷處理方法)

<a name="chapter1"/>
<h3 id="chapter1">NSDate - 表示一個絕對的時間點,表示公歷的GMT時間(格林威治時間),獨立于任何特定的歷法系統或時間區。 (2016-05-31 01:59:22 +0000)</h3>

1. NSDate - 初始化方法
- (id)init;
+ (id)date;
默認初始化,返回當前時間。

NSDate *date = [NSDate date];
// NSDate *date = [[NSDate alloc] init];    
NSLog(@"date = %@",date);
------------------------------------------------
date = 2016-05-31 02:21:22 +0000

- (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
以當前時間的偏移秒數來初始化。

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60];
// NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:60];
NSLog(@"now = %@",[NSDate date]);
NSLog(@"date = %@",date);
------------------------------------------------
now = 2016-05-31 02:39:58 +0000
date = 2016-05-31 02:40:58 +0000

- (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
+ (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
以2001-01-01 00:00:00 +0000 為時間基準,偏移秒數來初始化。

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:120];
// NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:120];
NSLog(@"date = %@",date);
------------------------------------------------
date = 2001-01-01 00:02:00 +0000

- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
以1970-01-01 00:00:00 +0000 為時間基準,偏移秒數來初始化。

NSDate *date = [NSDate dateWithTimeIntervalSince1970:60];
// NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:60];
NSLog(@"date %@",date);
------------------------------------------------
date = 1970-01-01 00:01:00 +0000

2. NSDate - 日期比較的方法

- (NSDate *)earlierDate:(NSDate *)anotherDate;
- (NSDate *)laterDate:(NSDate *)anotherDate;
將當前對象與另一個NSDate對象比較,返回較早/較晚的時間點,并以新NSDate對象的形式返回

NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:60];
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:30];
NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:30];
    
NSDate *earlierDate = [date1 earlierDate:date2];
    
NSDate *laterDate = [date1 laterDate:date2];
    
NSLog(@"earlierDate = %@", earlierDate);
NSLog(@"laterDate = %@", laterDate);

------------------------------------------------
earlierDate = 1970-01-01 00:00:30 +0000
laterDate = 1970-01-01 00:01:00 +0000

- (NSComparisonResult)compare:(NSDate *)other;
將當前對象與另一個NSDate對象比較,如果相同,返回0(NSOrderedSame);當前對象時間早于參數時間,返回-1(NSOrderedAscending);當前對象時間晚于參數時間,返回1(NSOrderedDescending)

NSComparisonResult compResult = [date2 compare:date3];
    
NSLog(@"compResult = %ld", compResult);

------------------------------------------------
compResult = 0

- (BOOL)isEqualToDate:(NSDate *)otherDate;
將當前對象與另一個NSDate對象比較,根據是否相同返回BOOL值。

BOOL isEqual = [date2 isEqualToDate:date3];

NSLog(@"isEqual = %d", isEqual);
------------------------------------------------
isEqual = 1

3. NSDate - 其他屬性和方法

NSDate.h

// 2001-01-01 00:00:00 到 1970-01-01 00:00:00 的時間間隔
#define NSTimeIntervalSince1970  978307200.0     

// 當前date對象于當前時間 ([NSDate date]) 的時間間隔
@property (readonly) NSTimeInterval timeIntervalSinceNow;   

// 當前date對象 與 1970-01-01 00:00:00 的時間間隔  (時間戳)
@property (readonly) NSTimeInterval timeIntervalSince1970;  

// 當前date對象 與 2001-01-01 00:00:00 的時間間隔
@property (readonly) NSTimeInterval timeIntervalSinceReferenceDate; 

// 0000-12-30 00:00:00 +0000
+ (NSDate *)distantPast;

// 4001-01-01 00:00:00 +0000
+ (NSDate *)distantFuture;


<a name="chapter2"/>
<h3 id="chapter2">NSLoale - 本地化信息,主要體現在"語言"和"區域格式"這兩個設置項。 (個人認為:不常用)</h3>

NSLocale與設備的設置有關:
系統的設置 > 通用 > 多語言環境 > 區域格式
系統的設置 > 通用 > 日期與時間 > 24小時制

1. + (id)systemLocale
根據官方文檔。設備默認的本地化信息。當不想用用戶設定的本地化信息時,使用systemLocale對象來設置你想要的效果。


2. + (id)currentLocale
根據官方文檔。當前用戶設定的本地化信息,即使修改了本地化設定,這個對象也不會改變。currentLocale取得的值可能被緩存在cache中。


3. + (id)autoupdatingCurrentLocale
根據官方文檔。當前用戶設定的本地化信息,此對象總是最新的本地化信息。

- (IBAction)_printLocale:(id)sender {

    NSLocale *currentLocale = [NSLocale currentLocale];
    NSLocale *systemLocale = [NSLocale currentLocale];
    NSLocale *autoupdatingCurrentLocale = [NSLocale autoupdatingCurrentLocale];
    
    NSLog(@"currentLocale = %@",currentLocale);
    NSLog(@"systemLocale = %@",systemLocale);
    NSLog(@"autoupdatingCurrentLocale = %@",autoupdatingCurrentLocale);
    
    NSLog(@"currentLocale.localeIdentifier = %@",currentLocale.localeIdentifier);
    NSLog(@"systemLocale.localeIdentifier = %@",systemLocale.localeIdentifier);
    NSLog(@"autoupdatingCurrentLocale.localeIdentifie = %@",autoupdatingCurrentLocale.localeIdentifier);
}
------------------------------------------------
******設置 -> 通用 -> 語言與地區 -> 高級 -> 語言 -> 簡體中文******
currentLocale = <__NSCFLocale: 0x7fc35150c570>
systemLocale = <__NSCFLocale: 0x7fc35150c570>
autoupdatingCurrentLocale = Auto-updating Locale <NSAutoLocale: 0x7fc35160fff0> [<__NSCFLocale: 0x7fc35150c570>]
currentLocale.localeIdentifier = zh_CN
systemLocale.localeIdentifier = zh_CN
autoupdatingCurrentLocale.localeIdentifie = zh_CN

真機手動設置
******設置 -> 通用 -> 語言與地區 -> 高級 -> 語言 -> 英文******
currentLocale = <__NSCFLocale: 0x7fc35179d5a0>
systemLocale = <__NSCFLocale: 0x7fc35179d5a0>
autoupdatingCurrentLocale = Auto-updating Locale <NSAutoLocale: 0x7fc3517a1e10> [<__NSCFLocale: 0x7fc35179d5a0>]
currentLocale.localeIdentifier = en_CN
systemLocale.localeIdentifier = en_CN
autoupdatingCurrentLocale.localeIdentifie = en_CN

我用系統為iOS9.3的真機和模擬器測試這三個初始化(單例)方法。個人得出的結果是:currentLocale、systemLocale內存地址一樣。autoupdatingCurrentLocale指向的貌似是拷貝currentLocale地址的內容新生成的地址。不做特殊處理,使用這三個是沒什么區別的。(建議使用autoupdatingCurrentLocale)


*4. - (id)initWithLocaleIdentifier:(NSString )string;
用標示符初始化本地化信息
如果你的應用程序在多個國家發布,那你就需要注意設置NSLocale。
比如:

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@”en_US”];

<a name="chapter3"/>
<h3 id="chapter3">NSTimeZone - 時區信息。 (個人認為:不常用) </h3>

NSTimeZone與設備的設置有關:
系統的設置 > 通用 > 日期與時間 > 時區
1. + (NSTimeZone *)systemTimeZone;
設備系統時區,不能代碼修改。(大伙的iPhone都是自動默認北京,如在手機‘設置’修改為倫敦,返回倫敦時區)


2. + (NSTimeZone *)localTimeZone;
本地時區,默認與systemTimeZone一致,可用代碼修改。


3. + (NSTimeZone *)defaultTimeZone;
默認時區,程序中定義的默認時區,默認與默認與systemTimeZone一致,可用代碼修改。

- (IBAction)_printTimeZone:(id)sender {
    NSLog(@"localTimeZone = %p  %@",[NSTimeZone localTimeZone], [NSTimeZone localTimeZone]);
    NSLog(@"systemTimeZone = %p  %@",[NSTimeZone systemTimeZone], [NSTimeZone systemTimeZone]);
    NSLog(@"defaultTimeZone = %p  %@",[NSTimeZone defaultTimeZone], [NSTimeZone defaultTimeZone]);
}
------------------------------------------------
******設置 -> 通用 -> 日期與時間 -> 時區 -> 北京******
localTimeZone = 0x157503690  Local Time Zone (Asia/Shanghai (GMT+8) offset 28800)
systemTimeZone = 0x1575a44e0  Asia/Shanghai (GMT+8) offset 28800
defaultTimeZone = 0x1575a44e0  Asia/Shanghai (GMT+8) offset 28800

真機手動設置
******設置 -> 通用 -> 日期與時間 -> 時區 -> 倫敦******
localTimeZone = 0x157503690  Local Time Zone (Europe/London (GMT+1) offset 3600 (Daylight))
systemTimeZone = 0x157502d50  Europe/London (GMT+1) offset 3600 (Daylight)
defaultTimeZone = 0x157502d50  Europe/London (GMT+1) offset 3600 (Daylight)

4. + (void)setDefaultTimeZone:(NSTimeZone *)aTimeZone;
用此方法可以改變localTimeZone、defaultTimeZone的值,(systemTimeZone不可改變)

- (IBAction)_printTimeZone:(id)sender {

    NSLog(@"localTimeZone = %p  %@",[NSTimeZone localTimeZone], [NSTimeZone localTimeZone]);
    NSLog(@"systemTimeZone = %p  %@",[NSTimeZone systemTimeZone], [NSTimeZone systemTimeZone]);
    NSLog(@"defaultTimeZone = %p  %@",[NSTimeZone defaultTimeZone], [NSTimeZone defaultTimeZone]);
    
    [NSTimeZone setDefaultTimeZone:[[NSTimeZone alloc] initWithName:@"America/Chicago"]];
    
    NSLog(@"localTimeZone = %p  %@",[NSTimeZone localTimeZone], [NSTimeZone localTimeZone]);
    NSLog(@"systemTimeZone = %p  %@",[NSTimeZone systemTimeZone], [NSTimeZone systemTimeZone]);
    NSLog(@"defaultTimeZone = %p  %@",[NSTimeZone defaultTimeZone], [NSTimeZone defaultTimeZone]);
}
------------------------------------------------
localTimeZone = 0x15fd05bf0  Local Time Zone (Asia/Shanghai (GMT+8) offset 28800)
systemTimeZone = 0x15fd0d310  Asia/Shanghai (GMT+8) offset 28800
defaultTimeZone = 0x15fd0d310  Asia/Shanghai (GMT+8) offset 28800

localTimeZone = 0x15fd05bf0  Local Time Zone (America/Chicago (GMT-5) offset -18000 (Daylight))
systemTimeZone = 0x15fd0d310  Asia/Shanghai (GMT+8) offset 28800
defaultTimeZone = 0x15fd87910  America/Chicago (GMT-5) offset -18000 (Daylight)

<a name="chapter4"/>
<h3 id="chapter4">NSCalendar - 日歷類,提供日歷的信息、大量日期計算的接口。 </h3>

創建與常見屬性

1. + (NSCalendar *)currentCalendar;
為當前用戶邏輯的日歷。即使修改了系統設置,這個對象也不會改變。currentCalendar取得的值可能被緩存在cache中。


2. + (NSCalendar *)autoupdatingCurrentCalendar;
為當前用戶邏輯的日歷。當每次修改系統日歷設定,其實例化的對象也會隨之改變。


*3. - (id)initWithCalendarIdentifier:(NSString )string;
指定標識符創建日歷對象。
(下表為部分歷法,詳情見Calendar Identifiers)

NSCalendarIdentifierGregorian 公歷 (陽歷)
NSCalendarIdentifierChinese 中國農歷
NSCalendarIdentifierBuddhist 佛教日歷
NSCalendarIdentifierIndian 印度日歷
NSCalendarIdentifierRepublicOfChina 中華民國日歷(中國臺灣)

4. 屬性 NSUInteger firstWeekday;
設置日歷中顯示的每周的第一天從星期幾開始,比如:1代表星期日開始,2代表星期一開始,3代表星期二開始,以此類推。默認值是1。 (此屬性在下文NSDateComponents還有說明)

calendar.firstWeekday默認值為1,代表日歷顯示星期日開始,日歷的表現形式為:

星期日

設置calendar.firstWeekday = 2,代表日歷顯示星期一開始,日歷的表現形式為:

星期一

設置calendar.firstWeekday = 3,代表日歷顯示星期二開始,日歷的表現形式為:

星期二

5. 屬性 NSUInteger minimumDaysInFirstWeek;
設置每年及每月第一周必須包含的最少天數,默認為1,即為第一周最少包括1天。


歷法計算

1. - (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date;
date對象由一個小的單位在一個大的單位里面的序數。
<a name="NSCalendarUnit"/>
NSCalendarUnit包含的值有:
<h3 id="NSCalendarUnit"></h3>

NSCalendarUnitEra               = 紀元單位,
NSCalendarUnitYear              = 年單位。值很大。(2016)年
NSCalendarUnitMonth             = 月單位。范圍為1-12
NSCalendarUnitDay               = 天單位。范圍為1-31
NSCalendarUnitHour              = 小時單位。范圍為0-24
NSCalendarUnitMinute            = 分鐘單位。范圍為0-60
NSCalendarUnitSecond            = 秒單位。范圍為0-60
NSCalendarUnitWeekday           = 星期單位。范圍為1-7 (一個星期有七天)
NSCalendarUnitWeekdayOrdinal    = 以7天為單位,范圍為1-5 (1-7號為第1個7天,8-14號為第2個7天...)
NSCalendarUnitQuarter           = 刻鐘單位。范圍為1-4 (1刻鐘等于15分鐘)
NSCalendarUnitWeekOfMonth       = 月包含的周數。最多為6個周
NSCalendarUnitWeekOfYear        = 年包含的周數。最多為53個周
NSCalendarUnitYearForWeekOfYear = 暫未深究...
NSCalendarUnitNanosecond        = 納秒單位
NSCalendarUnitCalendar          = 暫未深究...
NSCalendarUnitTimeZone          = 暫未深究...

下面一些例子:

  • 當小單位為NSCalendarUnitWeekday,大單位為NSCalendarUnitWeekOfMonth時(即某個日期在這一周是第幾天),根據firstWeekday屬性不同,返回的結果也不同.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *date = [NSDate date]; // 2016-06-07 星期二
    
calendar.firstWeekday = 1;
NSInteger count1 = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:date];
NSLog(@"firstWeekday = %ld, count1 = %ld", calendar.firstWeekday, count1);
    
calendar.firstWeekday = 2;
NSInteger count2 = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:date];
NSLog(@"firstWeekday = %ld, count2 = %ld", calendar.firstWeekday, count2);
------------------------------------------------
firstWeekday = 1, count1 = 3  // 以星期天為第一天,3即為星期二
firstWeekday = 2, count2 = 2  // 以星期一為第一天,2即為星期二

2. - (NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date;
date對象由一個小的單位在一個大的單位里面的取值范圍。


**3. - (BOOL)rangeOfUnit:(NSCalendarUnit)unit startDate:(NSDate * __nullable * __nullable)datep interval:(nullable NSTimeInterval )tip forDate:(NSDate )date;
原始時間點根據日歷單位計算出開始時間點、秒數差。如果能計算返回YES,不能返回NO。開始時間點跟秒數差通過參數返回。
unit - 日歷單位
datep - 開始時間,傳入date對象的指針地址,通過參數返回。
tip - 秒數差,傳入秒數的指針地址,通過參數返回。
date - 原始時間點參數


<a name="chapter5"/>
<h3 id="chapter5">NSDateComponents - 封裝date對象關于年月日時秒分、周、季度等信息的類</h3>

以下屬性與NSCalendarUnit對應

@property NSInteger era;
@property NSInteger year;
@property NSInteger month;
@property NSInteger day;
@property NSInteger hour;
@property NSInteger minute;
@property NSInteger second;
@property NSInteger nanosecond;
@property NSInteger weekday;
@property NSInteger weekdayOrdinal;
@property NSInteger quarter;
@property NSInteger weekOfMonth;
@property NSInteger weekOfYear;
@property NSInteger yearForWeekOfYear;

NSDateComponents本身沒有提供很多方法,它的相關方法大部分是其他類提供的。以下方法都是NSCalendar類的
1. - (nullable NSDate *)dateFromComponents:(NSDateComponents *)comps;
設置dateComponents,由calendar獲得特定的date對象。

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.year = 2020;
dateComponents.month = 10;
dateComponents.day = 01;

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [calendar dateFromComponents:dateComponents];
NSLog(@"%@",date);

------------------------------------------------
2020-09-30 16:00:00 +0000

2. - (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)date;
date對象根據指定的unitFlags,得到相應信息的dateComponents (沒有指定的unitFlags,對應的dateComponents是沒有值的)

NSUInteger unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday ;

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unit fromDate:date];

NSLog(@"year    = %ld",dateComponents.year);
NSLog(@"month   = %ld",dateComponents.month);
NSLog(@"day     = %ld",dateComponents.day);
NSLog(@"hour    = %ld",dateComponents.hour);
NSLog(@"minute  = %ld",dateComponents.minute);
NSLog(@"second  = %ld",dateComponents.second);
NSLog(@"weekday = %ld",dateComponents.weekday);
NSLog(@"(沒有設置此unit)weekdayOrdinal    = %ld",dateComponents.weekdayOrdinal);    
NSLog(@"(沒有設置此unit)quarter           = %ld",dateComponents.quarter);
NSLog(@"(沒有設置此unit)weekOfMonth       = %ld",dateComponents.weekOfMonth);
NSLog(@"(沒有設置此unit)weekOfYear        = %ld",dateComponents.weekOfYear);
NSLog(@"(沒有設置此unit)yearForWeekOfYear = %ld",dateComponents.yearForWeekOfYear);
    
------------------------------------------------
year    = 2016
month   = 6
day     = 8
hour    = 11
minute  = 35
second  = 41
weekday = 4
(沒有設置此unit)weekdayOrdinal    = 9223372036854775807
(沒有設置此unit)quarter           = 9223372036854775807
(沒有設置此unit)weekOfMonth       = 9223372036854775807
(沒有設置此unit)weekOfYear        = 9223372036854775807
(沒有設置此unit)yearForWeekOfYear = 9223372036854775807

3. - (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSCalendarOptions)opts;
根據指定的unitFlags,計算兩個date對應components信息的差值。

NSUInteger unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *todate = [NSDate date]; // 2016-06-08

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.year = 2016;
dateComponents.month = 01;
dateComponents.day = 01;

NSDate *earlierDate = [calendar dateFromComponents:dateComponents];

NSDateComponents *components = [calendar components:unit fromDate:earlierDate toDate:todate options:0];
NSLog(@"year差 = %ld",components.year);
NSLog(@"month差 = %ld",components.month);
NSLog(@"day差 = %ld",components.day);

------------------------------------------------
year差 = 0
month差 = 5
day差 = 7

<a name="chapter6"/>
<h3 id="chapter6">NSDateFormatter - 由特定的格式,用來在date和String之間轉換。 </h3>

1. - (NSString *)stringFromDate:(NSDate *)date;
2. - (nullable NSDate *)dateFromString:(NSString *)string;
NSDateFormatter的兩個最實用的方法是dateFromString和stringFromDate,前者將string經過格式化后變成NSDate對象,后者將NSDate對象格式化成string。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date1 = [NSDate date];
NSString *string1 = [formatter stringFromDate:date1];
NSLog(@"date1 = %@", date1);
NSLog(@"string1 = %@", string1);
    
NSString *string2 = @"2020-10-01 08:00:00 +0800";
NSDate *date2 = [formatter dateFromString:string2];
NSLog(@"date2 = %@", date2);
NSLog(@"string2 = %@", string2);
    
date1 = 2016-06-02 06:45:03 +0000
string1 = 2016-06-02 14:45:03 +0800
date2 = 2020-10-01 00:00:00 +0000
string2 = 2020-10-01 08:00:00 +0800

常用格式

y -- 年
假如是2013年,那么yyyy=2013,yy=13  

M -- 月 
假如是3月,那么M=3,MM=03,MMM=Mar,MMMM=March
假如是11月,那么M=11,MM=11,MMM=Nov,MMMM=November

d -- 這個月中的第幾天
假如是5號,那么d=5,dd=05
假如是15號,那么d=15,dd=15

a -- 上午(AM)/下午(PM)
    
G -- 紀元
一般會顯示公元前(BC)和公元(AD)

H -- 24小時制,顯示為0--23
假如是午夜00:40,那么H=0:40,HH=00:40

h -- 12小時制,顯示為1--12
假如是午夜00:40,那么h=12:40

m -- 分鐘
假如是5分鐘,那么m=5,mm=05
假如是45分鐘,那么m=45,mm=45

s -- 秒
假如是5秒鐘,那么s=5,ss=05
假如是45秒鐘,那么s=45,ss=45

z -- 時區
表現形式為GMT+08:00
 
Z -- 時區
表現形式為+0800

不常用的

w -- 年包含的周
假如是1月8日,那么w=2(這一年的第二個周)

W -- 月份包含的周(與日歷排列有關)
假如是2013年4月21日,那么W=4(這個月的第四個周)

F -- 月份包含的周(與日歷排列無關)
和上面的W不一樣,F只是單純以7天為一個單位來統計周,例如7號一定是第一個周,15號一定是第三個周,與日歷排列無關。

D -- 在這一年中的第幾天
假如是1月20日,那么D=20(這一年的第20天)
假如是2月25日,那么D=31+25=56(這一年的第56天)

E -- 星期 
假如是星期五,那么E=Fri,EEEE=Friday

K -- 12小時制,顯示為0--11
假如是午夜00:40,那么K=0:40,KK=00:40

k -- 24小時制,顯示為1--24
假如是午夜00:40,那么k=24:40

S -- 毫秒   
一般用SSS來顯示
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,578評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,701評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,691評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,974評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,694評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,026評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,015評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,193評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,719評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,442評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,668評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,151評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,846評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,255評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,592評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,394評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內容