首页 技术 正文
技术 2022年11月7日
0 收藏 552 点赞 1,141 浏览 5642 个字

本文转载至 http://blog.sina.com.cn/s/blog_9cd1705d0102v5x4.html

 

主要有以下类:

NSDate — 表示一个绝对的时间点
NSTimeZone — 时区信息
NSLocale — 本地化信息
NSDateComponents — 一个封装了具体年月日、时秒分、周、季度等的类
NSCalendar — 日历类,它提供了大部分的日期计算接口,并且允许您在NSDate和NSDateComponents之间转换
NSDateFormatter — 用来在日期和字符串之间转换

NSDate

NSDate用来表示公历的GMT时间(格林威治时间)。 有下面几种初始化方法:

1. – (id)init

默认初始化,返回当前时间,也可以直接调用类方法 +(id)date

NSDate *date = [[NSDate alloc] init];
//NSDate *date = [NSDate date];
NSLog(@"print date is %@",date);

将打印出计算机当前时间:2013-03-04 08:57:20 +0000

2. – (id)initWithTimeIntervalSinceNow:(NSTimeInterval)seconds

以当前时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds

NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:20];
//NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];
NSLog(@"print date is %@",date);

假如当前时间是2013-03-04 08:57:20 +0000,那么初始化后得到的时间是2013-03-04 08:57:40 +0000

3. – (id)initWithTimeIntervalSince1970:(NSTimeInterval)seconds

以GMT时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds

NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:-20];
//NSDate *date = [NSDate dateWithTimeIntervalSince1970:-20];
NSLog(@"print date is %@",date);

得到的时间是格林威治时间往前20秒,将打印出:1969-12-31 23:59:40 +0000

4. – (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds

以2001-1-1 0:0:0的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds

NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:80];
//NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:80];
NSLog(@"print date is %@",date);

将打印出:2001-01-01 00:01:20 +0000

5. – (id)initWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)refDate

以基准时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)date

NSDate *date1 = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:20];
NSLog(@"print date1 is %@",date1);NSDate *date2 = [[NSDate alloc] initWithTimeInterval:10 sinceDate:date1];
//NSDate *date2 = [NSDate dateWithTimeInterval:10 sinceDate:date1];
NSLog(@"print date2 is %@",date2);

第一个基准时间是2001-01-01 00:00:20 +0000,根据基准时间偏移10秒的结果是2001-01-01 00:00:30 +0000

6.  + (id)distantPast 与 + (id)distantFuture

这两个是类方法,分别用来返回一个极早的时间点和一个极晚的时间点

NSDate *date = [NSDate distantFuture];
NSLog(@"future date is %@",date);NSDate *date2 = [NSDate distantPast];
NSLog(@"past date is %@",date2);

distantPast将返回:0001-12-30 00:00:00 +0000,distantFuture将返回:4001-01-01 00:00:00 +0000

NSDate的常用对象方法:

1. -(id)dateByAddingTimeInterval:(NSTimeInterval)seconds

返回以当前NSDate对象为基准,偏移多少秒后得到的新NSDate对象。(旧方法 – (id)addTimeInterval:(NSTimeInterval)seconds已被弃用)

NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [date dateByAddingTimeInterval:-20];
NSLog(@"%@",date2);

2. – (BOOL)isEqualToDate:(NSDate *)anotherDate

将当前对象与参数传递的对象进行比较,根据是否相同返回BOOL值

NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [NSDate dateWithTimeInterval:0 sinceDate:date];
BOOL isEqual = [date isEqualToDate:date2];
NSLog(@"%i",isEqual);

3. – (NSDate *)earlierDate:(NSDate *)anotherDate 与 – (NSDate *)laterDate:(NSDate *)anotherDate

比较两个NSDate对象,返回较早/较晚的时间点,并以新NSDate对象的形式返回

NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];

NSDate *date2 = [NSDate dateWithTimeInterval:-50 sinceDate:date];NSDate *date3 = [date earlierDate:date2];
NSLog(@"earlier date is %@",date3);NSDate *date4 = [date laterDate:date2];
NSLog(@"later date is %@",date4);

4. – (NSComparisonResult)compare:(NSDate *)anotherDate

将当前对象与参数传递的对象进行比较,如果相同,返回0(NSOrderedSame);对象时间早于参数时间,返回-1(NSOrderedAscending);对象时间晚于参数时间,返回1(NSOrderedDescending)

NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [NSDate dateWithTimeInterval:-50 sinceDate:date];NSInteger result = [date compare:date2];
NSLog(@"%i",result);

5. – (NSTimeInterval)timeIntervalSince1970

返回当前对象时间与1970-1-1 0:0:0的相隔秒数,也可以这样理解:从1970-1-1 0:0:0开始,经过多少秒到达对象指定时间。

NSDate *date = [NSDate dateWithTimeIntervalSince1970:50];
NSInteger seconds = [date timeIntervalSince1970];
NSLog(@"%i",seconds);

将返回结果50

6. – (NSTimeInterval)timeIntervalSinceReferenceDate

返回当前对象时间与2001-1-1 0:0:0的相隔秒数,也可以这样理解:从2001-1-1 0:0:0开始,经过多少秒到达对象指定时间。

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:-30];
NSInteger seconds = [date timeIntervalSinceReferenceDate];
NSLog(@"%i",seconds);

将返回结果-30,负数代表从2001-1-1 0:0:0开始,倒退30秒到达当前时间。

7. – (NSTimeInterval)timeIntervalSinceNow

返回当前对象时间与客户端时间的相隔秒数,也可以这样理解:从客户端当前时间开始,经过多少秒到达对象指定时间。

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:100];
NSInteger seconds = [date timeIntervalSinceNow];
NSLog(@"%i",seconds);

经测试返回了结果99,但初始化时提供的参数是100。这可能是因为第一句初始化代码到第二句计算代码之间有个1秒内的延时,所以计算时的客户端时间比初始化时的客户端时间快了1秒。

8. – (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate

返回当前对象时间与参数传递的对象时间的相隔秒数,也可以这样理解:从参数时间开始,经过多少秒到达对象执行时间。

NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *date2 = [NSDate dateWithTimeInterval:50 sinceDate:date];
NSInteger seconds = [date timeIntervalSinceDate:date2];
NSLog(@"%i",seconds);

将返回结果-50,date为1970-1-1 0:0:0,date2为1970-1-1 0:0:50,从date2的时间开始,倒退50秒到达date的时间。

NSTimeZone

NSTimeZone表示时区信息。 有下面几种初始化方法:

1. + (id)timeZoneWithName:(NSString *)aTimeZoneName / – (id)initWithName:(NSString *)aName

根据时区名称初始化。可以调用NSTimeZone的类方法 + (NSArray *)knownTimeZoneNames来返回所有已知的时区名称。

NSTimeZone *zone = [[NSTimeZone alloc] initWithName:@"America/Chicago"];
//NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"America/Chicago"];
NSLog(@"%@",zone);

打印出:America/Chicago (CST) offset -21600

2. + (id)timeZoneWithAbbreviation:(NSString *)abbreviation

根据时区缩写初始化。例如:EST(美国东部标准时间)、HKT(香港标准时间)

NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
NSLog(@"%@",zone);

打印出:Asia/Hong_Kong (HKT) offset 28800

3. + (NSTimeZone *)systemTimeZone

返回系统时区

NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSLog(@"%@",zone);

假如时区是上海,打印出的时区信息将会是:Asia/Shanghai (CST (China)) offset 28800,28800代表相对于GMT时间偏移的秒数,即8个小时。(8*60*60)

4. + (NSTimeZone *)localTimeZone

返回本地时区,与systemTimeZone的区别在于:本地时区可以被修改,而系统时区不能修改。

http://www.cnblogs.com/wayne23/archive/2013/03/25/2981009.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,038
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,524
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,372
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,152
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,785
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,868