NSDate NSTimeZone NSDateFormatter

Sep 3, 2014


NSDate

封装一个时间对象,不关乎格式、时区、本地信息等。也就是说,一个NSDate对象,它只是一个时间,不包含时区、地区等信息。

NSTimeZone

时区,抽象类,代表一个区域的时间。给一些对象提供属性,比如:NSDateFormate、NSCalendar、UIDatePicker等

系统时区

+ systemTimeZone系统时区,如果不能获取系统时区,将会返回GMT时区。调用该方法获时区后,会进行缓存,如果系统时间发生改变,再次调用该方法,不会获取到最新的系统时区,而是得到之前的时区。可以调用+ resetSystemTimeZone清除缓存。

默认时区

+ setDefaultTimeZone:设置默认时区。

通过 + defaultTimeZone+ localTimeZone都可以获取默认时区,如果没有设置默认时区,会得到系统时区。

两者区别:

NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone];

NSTimeZone * localTimeZone = [NSTimeZone localTimeZone];

defaultTimeZone localTimeZone 都表示默认时区。此时如果我们更改默认时区,即调用+ setDefaultTimeZone:方法,defaultTimeZone 不会发生变化,而localTimeZone 也跟着发生变化。

常见用法

//获取系统可以识别的时区名字
NSArray *zoneNames = [NSTimeZone knownTimeZoneNames];
//通过时区名字创建时区
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
//通过GTM时差创建时区
NSTimeZone *timeZone1 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*8];

NSLocale

将语言、文化、公约和标准封装起来的抽象类,比如封装十进制符号,货币符号、日期显示格式等。

代码:

//系统可识别的地区identifier
NSArray *locales = [NSLocale availableLocaleIdentifiers];
for (NSString *identifier in locales) {
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:identifier];
    //根据NSLocale:locale 和 key:NSLocaleIdentifier 格式化value:identifier 得到格式化后的NSString
    NSLog(@"%@",[locale displayNameForKey:NSLocaleIdentifier value:identifier]);
}
//根据identifier创建,zh表示中文,Hans表示简体,CN表示中国
NSLocale *newLocale = [NSLocale localeWithLocaleIdentifier:@"zh_Hans_CN"];
//获取系统语言偏好设置
NSArray *languages = [NSLocale preferredLanguages];

NSDateFormatter

系统自带的格式:

//系统自带格式化字符串
typedef NS_ENUM(NSUInteger, NSDateFormatterStyle) {
    NSDateFormatterNoStyle = kCFDateFormatterNoStyle,//无输出
    NSDateFormatterShortStyle = kCFDateFormatterShortStyle,//16/4/26 上午10:49
    NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,//2016年4月26日 上午10:49:08
    NSDateFormatterLongStyle = kCFDateFormatterLongStyle, //2016年4月26日 GMT+8 上午10:48:39
    NSDateFormatterFullStyle = kCFDateFormatterFullStyle //2016年4月26日 星期二 中国标准时间 上午10:51:11
};
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateStyle = kCFDateFormatterFullStyle;
formatter.timeStyle = kCFDateFormatterFullStyle;
NSDate *now = [NSDate new];
NSString* outputString = [formatter stringFromDate:now];

自定义格式:

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd a HH:mm:ss EEEE"; //2016-04-26 上午 10:56:25 星期二
NSDate *now = [NSDate new];
NSString* outputString = [formatter stringFromDate:now];
NSLog(@"%@ --- %@",outputString,formatter.timeZone);

日期字段格式表

下表引用自ICU User Guide;详细信息可以参考unicode

Date Field Symbol Table

Symbol Meaning Example(s)
G era designator G, GG, or GGG
GGGG
GGGGG
AD
Anno Domini
A
y year yy
y or yyyy
96
1996
Y year of "Week of Year" Y 1997
u extended year u 4601
U cyclic year name, as in Chinese lunar calendar U 甲子
r related Gregorian year r 1996
Q quarter Q
QQ
QQQ
QQQQ
QQQQQ
2
02
Q2
2nd quarter
2
q Stand Alone quarter q
qq
qqq
qqqq
qqqqq
2
02
Q2
2nd quarter
2
M month in year M
MM
MMM
MMMM
MMMMM
9
09
Sep
September
S
L Stand Alone month in year L
LL
LLL
LLLL
LLLLL
9
09
Sep
September
S
w week of year w
ww
27
27
W\ week of month W 2
d day in month d
dd
2
02
D day of year D 189
F day of week in month F 2 (2nd Wed in July)
g modified julian day g 2451334
E day of week E, EE, or EEE
EEEE
EEEEE
EEEEEE
Tue
Tuesday
T
Tu
e local day of week
example: if Monday is 1st day, Tuesday is 2nd )
e or ee
eee
eeee
eeeee
eeeeee
2
Tue
Tuesday
T
Tu
c Stand Alone local day of week c or cc
ccc
cccc
ccccc
cccccc
2
Tue
Tuesday
T
Tu
a am/pm marker a pm
h hour in am/pm (1~12) h
hh
7
07
H hour in day (0~23) H
HH
0
00
k hour in day (1~24) k
kk
24
24
K hour in am/pm (0~11) K
KK
0
00
m minute in hour m
mm
4
04
s second in minute s
ss
5
05
S fractional second - truncates (like other time fields)
to the count of letters when formatting. Appends
zeros if more than 3 letters specified. Truncates at
three significant digits when parsing.
S
SS
SSS
SSSS
2
23
235
2350
A milliseconds in day A 61201235
z Time Zone: specific non-location z, zz, or zzz
zzzz
PDT
Pacific Daylight Time
Z Time Zone: ISO8601 basic hms? / RFC 822
Time Zone: long localized GMT (=OOOO)
TIme Zone: ISO8601 extended hms? (=XXXXX)
Z, ZZ, or ZZZ
ZZZZ
ZZZZZ
-0800
GMT-08:00
-08:00, -07:52:58, Z
O Time Zone: short localized GMT
Time Zone: long localized GMT (=ZZZZ)
O
OOOO
GMT-8
GMT-08:00
v Time Zone: generic non-location
(falls back first to VVVV)
v
vvvv
PT
Pacific Time or Los Angeles Time
V Time Zone: short time zone ID
Time Zone: long time zone ID
Time Zone: time zone exemplar city
Time Zone: generic location (falls back to OOOO)
V
VV
VVV
VVVV
uslax
America/Los_Angeles
Los Angeles
Los Angeles Time
X Time Zone: ISO8601 basic hm?, with Z for 0
Time Zone: ISO8601 basic hm, with Z
Time Zone: ISO8601 extended hm, with Z
Time Zone: ISO8601 basic hms?, with Z
Time Zone: ISO8601 extended hms?, with Z
X
XX
XXX
XXXX
XXXXX
-08, +0530, Z
-0800, Z
-08:00, Z
-0800, -075258, Z
-08:00, -07:52:58, Z
x Time Zone: ISO8601 basic hm?, without Z for 0
Time Zone: ISO8601 basic hm, without Z
Time Zone: ISO8601 extended hm, without Z
Time Zone: ISO8601 basic hms?, without Z
Time Zone: ISO8601 extended hms?, without Z
x
xx
xxx
xxxx
xxxxx
-08, +0530
-0800
-08:00
-0800, -075258
-08:00, -07:52:58
' escape for text ' (nothing)
' ' two single quotes produce one ' ' '