与Java中类似。
NADate;
代表日期和时间。
NSDate* date1=[NSDate date];------------获取当前的日期时间等
NSDate*date2=[[NSDate alloc]initWithTimeIntervalSinceNow:3600*24]--------------一天后的日期,-3600*24代表一天前的日期
NSDate*date3=[NSDate dateWithTimeIntervalSince1970:3600*24*366*20];------------------从1970开始,20年后的日期
NSLocale* cn=[NSLocale currentLocal];
[date1 descriptionWithLocal:cn]------------获取NSDate在当前NSLocal下对应的字符串
[date1 earlierDate:date2]-------------------考前的日期。laterDate--------------靠后的日期
switch([date1 compare:date2]){
case NSOrderedAscending:
----date1靠前
case NSOrderedSame:
----相同
case NSOrderDescending:
----date3靠前
}
[date1 timeIntervalSinceDate;date2]--------------获取时间差-----秒
[date1 timeIntervalSinceNow]----------------时间与现在相差多少秒
日期格式化器 NSDateFormatter-----------------------类似于Java中的simpledateformat
NSDateFormatter的setDateStyle/setTimeStyle用以设置格式化日期或者时间的风格。
自带的风格:
NSDateFormatterNoStyle----不显示日期、时间
NSDateFormatterShortStyle----显示"短"日期和时间
NSDateFormatterMediumStyle----不显示“中等”日期和时间
NSDateFormatterLongStyle----不显示“长”日期和时间
NSDateFormatterFullStyle----不显示“完整”日期和时间
如果需要自定义stytle则使用NSDateFormatter的setDateFormat方法
NSDateFormat* df=[[NSDateFormatter alloc] init];
[df setDateFormat:@"公元yyyy年MM月DD日HH时mm分"];
NSDate* date1=[NSDate date];
[df stringFromDate:date1];
如果需要把NSDate转化为NSString则需要使用NSDateFormatter的stringFromDate方法;反之需要调用dateFromString方法。
NSString* s=@"2016-025-11";
NSDateFormatter* df=[[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSdate*date2=[df dateFromString: s];
日历 NSCalendar 日期组件NSDateComponents
日历类的两个方法
(NSDateComponents*)components:fromDate:----------------------------从NSDate中提取年、月、日、时分秒-----不是类方法
dateFromComponents:(NSDateComponents*)comps----------------------使用NSDateComponents获取NSDate对象
NSDateComponents.year-----------------------------------获取年月日 该方法不是类方法
定时器 NSTimer
实例化---调用NSTimer的scheduledTimerWithInterval:repeats:或者scheduledTimerWithTimeInterval:target :selector:userInfo:repeats来实例化对象
timeInterval用以表示时间间隔
invocation或者target和selecttor用以指定重复执行的任务。如果指定target和selector参数,则指定用某个对象的特定方法作为重复执行的任务;如果指定invocation参数,该参数需要传入一个NSInvocation对象,该NSInvocation对象也是封装target和selector,也是指定用某个对象的特定方法作为重复执行的任务 。
userInfo用以传递额外的附加信息
repeats 制定一个BOOL值,控制是否需要重复执行。
销毁定时器,执行invalidate方法。