Objective-C学习笔记_NSDate、NSDateFormatter
2015-08-09 17:19
465 查看
一、掌握NSDate的使⽤
在iOS开发中,NSDate的使用场景很多,例如,聊天程序里的时间显示(几分钟前、几小时前、几天前)的计算;网络请求中的时间戳(计算当前时间距离1970年1⽉1⽇的秒数)。NSDate是Cocoa中用于处理日期和时间的基础类,封装了某⼀给定的时刻(含日期、时间、时区)。使用+date方法获取当前时间。例如:
NSDate *nowDate = [NSDate date];注意
NSLog(@“%@”, nowDate);⽆论你是哪个时区的时间,打印的总是0时区时间。
NSTimeinterval
NSTimeInterval(double类型),用来表⽰以秒为单位的时间间隔。可以使用-initWithTimeIntervalSinceNow:方法传入⼀个NSTimeInterval参数,创建一个NSDate对象。
例如:
NSDate *tomorrowDate = [[NSDate alloc] initWithTimeIntervalSinceNow: 24 * 60 * 60];
NSDate *yesterdayDate = [[NSDate alloc] initWithTimeIntervalSinceNow: -1 * 24 * 60 * 60];取两个时间对象的间隔:
NSTimeinterval = [tomorrowDate timeIntervalSinceDate: yesterdayDate];
具体实现代码如下:
#pragma mark ** NSDate 日期类 NSDate *date = [NSDate date]; /* 0时区的时间 */ NSLog(@"%@", date); /* 0时区的时间 + 秒数 = 当前时区的时间 */ /* 获取当前的时区 */ NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSLog(@"%@", zone); NSTimeZone *zone2 = [NSTimeZone localTimeZone]; NSLog(@"%@", zone2); /* 获取和0时区相差的秒数 */ NSInteger seconds = [zone secondsFromGMTForDate:date]; /* 当前时间 */ NSDate *localDate = [NSDate dateWithTimeIntervalSinceNow:seconds]; NSLog(@"localDate: %@", localDate); NSDate *local2 = [NSDate dateWithTimeIntervalSinceNow:8 * 3600]; NSLog(@"local2: %@", local2); /* 明天时间 */ NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:8 * 3600 + 24 * 3600]; NSLog(@"tomorrow: %@", tomorrow); /* 昨天时间 */ NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:8 * 3600 - 24 * 3600]; NSLog(@"yesterday: %@", yesterday); #pragma mark ** NSTimeInterval 时间间隔类 NSTimeInterval timeInterval = [tomorrow timeIntervalSinceDate:yesterday]; NSLog(@"timeInterval: %lf", timeInterval); NSTimeInterval timeInterval2 = [localDate timeIntervalSince1970]; NSLog(@"timeInterval2: %lf", timeInterval2); /* 快速获取当前时区的时间 */ NSDate *dateLocal = [NSDate dateWithTimeIntervalSinceNow:[NSTimeZone localTimeZone].secondsFromGMT]; NSLog(@"dateLocal: %@", dateLocal);
二、掌握NSDateFormatter的使⽤
NSDateFormatter是iOS中的日期格式类,功能是实现NSString和NSDate的互转。常见的时间格式化字符串有:y 年、M 月份、d 天、H 小时(24小时制)、h(12小时制)、am | pm 上午| 下午、m 分钟、s 秒数等。指定的日期格式:NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate转NSString
日期转化为字符串:NSDateFormatter*formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; NSString *dateString = [formatter stringFromDate: [NSDate date]];
NSString转NSDate
时间字符串转化为相对应的⽇期:NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *dateStr = @”2008-08-08 20:08:08”; NSDate *date = [formatter dateFromString:dateStr];
具体实现代码如下:
#pragma mark ** NSDateFormatter /* y 年 * M 月 * d 日 * h 小时(12), H(24) * m 分 * s 秒 * a 显示上\下午 * z 时区 * eee 星期 * G 公元 */ /* NSDate 转 字符串 */ /* 方法1, 字符串format方法 */ NSString *string1 = [NSString stringWithFormat:@"%@", dateLocal]; NSLog(@"string1: %@", string1); /* 方法2, NSDateFormatter格式 */ /* 步骤: 1. 新建一个格式 * 2. 按照格式进行转换 */ NSDateFormatter *format1 = [[NSDateFormatter alloc] init]; [format1 setAMSymbol:@"AM"]; [format1 setPMSymbol:@"PM"]; [format1 setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"]; NSString *string2 = [format1 stringFromDate:date]; NSLog(@"string2: %@", string2); /* NSDateFormat 自动转换为当前系统时区 */ /* 字符串 转 NSDate */ NSString *string3 = @"2015-04-16 19:44:16"; NSDate *date11 = [format1 dateFromString:string3]; NSLog(@"date11: %@", date11); /* 练习: 定义一个NSDateFormatter, 设置合适的格式 * 将字符串@“2014年05⽉01日 10点23分18秒”转换为NSDate对象。 */ NSDateFormatter *exFormat = [[NSDateFormatter alloc] init]; [exFormat setDateFormat:@"yyyy年MM月dd日 HH点mm分ss秒"]; NSString *timeStr = @"2014年05月01日 10点23分18秒"; NSDate *dateStr = [[exFormat dateFromString:timeStr] dateByAddingTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT]; NSLog(@"timeStrDate: %@", dateStr);
相关文章推荐
- Object类、String类、StringBuffer类、StringBuilder类
- Objective-C学习笔记_字典、集合、数组排序
- Objective-C--@property,@synthesize关键字介绍
- 错误解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
- var, object, dynamic的区别以及使用(转载)
- objective-C 中两种实现动画的方法(转)
- Objective-C学习笔记_NSString、NSArray、NSNumber
- Python自然语言处理 NLTK包中的 text3.generate() 命令出错 'Text' object has no attribute 'generate'
- 【Objective-C编程开发】关于子类重写父类初始化方法的争论
- iOS开发核心语言Objective C —— 面向对象思维、setter和getter方法及点语法
- objective-c 中 . 运算符详解
- uva 1161 Objective: Berlin (最大流)
- iOS Objective-C 身份证号码校验
- iOS开发(Objective-C)常用库索引
- 代理作用之一代理传值:正向传值
- swift中使用object-c代码
- 黑马程序员--- objective-c 类的继承
- OC语法——Object-C retain、copy、mutableCopy的详细分析
- 从JSONModel看Objective-C的反射机制
- 《Objective-C基础教程》第6章 源文件组织