iOS 日期处理的相关类

GitHub: https://github.com/GardenerYun
Email: gardeneryun@foxmail.com
简书博客地址: http://www.jianshu.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阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容