GitHub: https://github.com/GardenerYun
Email: gardeneryun@foxmail.com
簡書博客地址: http://www.lxweimin.com/users/8489e70e237d/latest_articles
如有問題或建議請聯系我,我會馬上解決問題~ (? ??_??)?
索引
簡書不支持目錄跳轉,有需要的請移步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來顯示