我的iOS学习历程 - OC第七天
来源:互联网 发布:linux修改文件所有者 编辑:程序博客网 时间:2024/06/08 16:24
今天主要讲的是NSDate的使用(各种方法的使用) 还有类目的使用
最重要的是协议的使用步骤(由于代码的类不好写上来看 请点击下载源码),在后面点击下载(点击进入下载页面)
(main中使用的类目代码在最下层)
系统内表示时间日期的类 NSDateNSDate *date = [NSDatedate];
打印出来的是格林威治时间0时区
咱们在东八区
距离现在 n秒之后的后期
NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:24 *3600];距离2001-1-1 00.00.00 n秒之后的时间
NSDate *date2 = [NSDate dateWithTimeIntervalSinceReferenceDate:3600];NSLog(@"%@",date2);距离1970-1-1 00:00:00 n秒之后的时间
NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:3600];练习题:计算当前时间和⼀个固定时间的差值,如果差值在60秒内,输出“刚刚”,如果在60秒外3600秒内,输出“xx分钟前”,如果3600秒外,3600*24秒内,输出“xx⼩时前
NSDate *date = [NSDate date];NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:3000];NSTimeInterval interval = [date1timeIntervalSinceDate:date];NSLog(@"%.0f",interval);if (interval <=60 ) { NSLog(@"刚刚");}elseif (interval <=3600){ NSLog(@"%.0f分钟前",interval /60);}elseif(interval >3600 *24){ NSLog(@"%.0f小时前",interval /3600);}
按你喜欢的格式来输出时间
继承一个抽象类
抽象类的特点:抽象类本身不实现什么具体功能
具体的功能由其子类去实现
NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];// 添加一个格式// 2015年 11月11日 20-00-00// 先创建一个NSDateFormatter类型数据用setDateFormat改变格式创建一个时间然后使用NSDateFormatter中的stringFromDate方法转化为字符串格式输出[dateformat setDateFormat:@"yyyy年 MM月dd日 HH-mm-ss"];// 把当前时间转化为按上面的格式输出 NSDate *date = [NSDate date]; NSString *str = [dateformat stringFromDate:date ]; NSLog(@"%@",str);
把一个日期时间的字符串转化为NSDate打印出来
NSDateFormatter *dateformat1 = [[NSDateFormatter alloc]init];NSString *str1 =@"2012.04.01 20:21:56";[dateformat1 setDateFormat:@"yyyy.MM.dd HH:mm:ss"];// 创建一个时区NSTimeZone *zone1 = [NSTimeZone timeZoneForSecondsFromGMT:0];// 设置一下时区[dateformat1 setTimeZone:zone1];NSDate *date1 = [dateformat1 dateFromString:str1];NSLog(@"%@",date1);
时区类
NSTimeZone *zone = [NSTimeZone systemTimeZone];
定义⼀个NSDateFormatter,设置合适的格式。将字符串@“2014年05⽉01⽇10点23分18秒”转换为NSDate对象。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH点mm分ss秒"];NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0];[dateFormatter setTimeZone:zone];NSString *str =@"2014年05月01日 10点23分18秒";NSDate *date = [dateFormatter dateFromString:str];NSLog(@"%@",date);
/* ***********************************类的扩展******************************** */
类目
特点:可以给看不到实现的系统类添加方法并且添加的方法使用系统类的对象或者类名直接就可以调用
切记:
1.只能添加方法不能添加实例变量
2.类目中添加的方法相当于直接添加到系统类中是可以被继承的
[NSString printfEnergy];// 使用NSString的子类调用类目中的方法[NSMutable String printfEnergy]; // 练习: 使⽤Category为NSDate类添加⼀个类⽅法(dateWithDateString:),实现将下述字符串转换为NSDate对象。将@“20140402142850”转换为NSDate。NSString *str =@"20140402142850";[NSDate dateWithDateString:str];
以下是类目的代码
NSDate+DateBecomeString.h
#import <Foundation/Foundation.h>@interface NSDate (DateBecomeString)+(void)dateWithDateString:(NSString *)DateString;@endNSDate+DateBecomeString.m
#import "NSDate+DateBecomeString.h"@implementation NSDate (DateBecomeString)+(void)dateWithDateString:(NSString *)DateString{ NSDateFormatter *format = [[NSDateFormatter alloc]init]; NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0]; [format setDateFormat:@"yyyyMMddHHmmss"]; [format setTimeZone:zone]; NSDate *date = [format dateFromString:DateString]; NSLog(@"%@",date);}@end
NSString+PrintfString.h
<span style="font-size:18px;color:#333333;">#import <Foundation/Foundation.h>@interface NSString (PrintfString)// 给字符串添加一个类方法// 打印你的潜力有多大+(void)printfEnergy;</span>
NSString+PrintfString.m
@implementation NSString (PrintfString)+(void)printfEnergy{ NSLog(@"你的潜力有多大");}
0 0
- 我的iOS学习历程 - OC第七天
- 我的iOS学习历程 - OC第一天
- 我的iOS学习历程 - OC第三天
- 我的iOS学习历程 - OC第五天
- 我的iOS学习历程 - OC第八天
- 我的iOS学习历程 - OC第九天
- 我的iOS学习历程 - OC第二天
- 我的iOS学习历程 - OC第四天
- 我的iOS学习历程 - OC第六天
- 我的iOS学习历程 - OC第十天
- 学习iOS的第七天
- OC学习第七天_1NSDate NStimeFormatter NSTimeZone
- 学习的第七天
- 我的IMU第七天
- 我的iOS学习历程 - UITextField UIButton
- 我的iOS学习历程 - 自定义视图
- 我的iOS学习历程 - MVC模式
- 我的iOS学习历程 - TargetAction
- Codevs_P3186 队列练习2
- 选项卡的项目不能完全显示(source insight 的设置问题)
- 杂谈:IT中的寒冬潮是否来了?
- Objective-C中的属性指示符
- 单例模式实现
- 我的iOS学习历程 - OC第七天
- 二叉树的各种操作
- c++虚函数
- Linux用户管理
- 从程序员到CTO的Java技术路线图 (转自安卓巴士)
- android 中Service 和activity之间的数据传递的几种方法
- 好坑啊 NYOJ303 序号互换
- T-SQL学习之路之数据库完整性之参规则
- Selenium Webdriver JS事件在各浏览器的区别,彻底解决Selenium鼠标悬停 Mouseover和hover问题