NSDate的計(jì)算問題,日期計(jì)算,時(shí)區(qū)問題,NSTimer
一.NSDate的計(jì)算問題
NSTimeInterval 是一個(gè)以秒為單位的時(shí)間片。
1.可以用initWithTimeIntervalSinceNow方法傳入一個(gè)NSTimeInterval對象,創(chuàng)建一個(gè)NSDate對象。
- NSDate * tomorrow =[[NSDate alloc]initWithTimeIntervalSinceNow:24*60*60];
- SDate * yesterday = [[NSDate alloc]initWithTimeIntervalSinceNow:-24*60*60];
2.可以使用+dateWithTimeIntervalSinceNow:方法來創(chuàng)建一個(gè)NSDate對象
- NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];
- SDate * tomorrow = [NSDate dateWithTimeIntervalSinceNow:24*60*60];
3.使用-dateByAddingTimeInterval方法創(chuàng)建NSDate對象
- NSDate * now = [NSDate date];
- NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
- NSDate * anHourAfter = [now dateByAddingTimeInterval:60*60];
二、日期的比較
1.日期可以進(jìn)行比較以確定大小或相等,也可以確定兩個(gè)日期之間的時(shí)間間隔。兩個(gè)日期的間隔時(shí)間差可以使用-timeIntervalSinceDate:方法來計(jì)算
- NSDate * now = [NSDate date];
- NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
- NSTimeInterVal timeBetween = [now timeIntervalSinceDate:anHourAgo];
- NSLog(@”%f”,timeBetween);
2.日期比較也可以使用-timeIntervalSinceNow方法獲取和當(dāng)前的時(shí)間間隔
- NSDate * anHourago = [NSDate dateWithTimeIntervalSinceNow;-60*60];
- NSTimeInterval interval = [anHourAgo timeIntervalSinceNow];
- NSLog(@”%f”,interval);
3.NSDate還提供了-laterDate、-earlierDate和compare方法來比較日期
- NSDate * now = [NSDate date];
- NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
- NSDate *result1 = [now laterDate:anHourAgo];
- NSDate * result2 = [now earlierDate:anHourAgo];
- NSComparisonResult result3 = [now compare:anHourAgo];
三、時(shí)區(qū)問題:
1. 處理日期和時(shí)間經(jīng)常遇到的一個(gè)問題計(jì)算時(shí)區(qū)問題。Foundation框架提供NSTimeZone來指定日 歷對象的時(shí)區(qū)。+knowTimeZoneNamespace可以列舉出所有時(shí)區(qū);+timeZoneWithName可以指定名稱參數(shù)創(chuàng)建一個(gè)時(shí) 區(qū);+timeZoneWithAbbreviation可以指定時(shí)區(qū)縮寫創(chuàng)建一個(gè)時(shí)區(qū)
- NSTimeZone * zone1 = [NSTimeZone timeZoneWithAbbreviation:@”PRC”];
- STimeZone * zone2 = [NSTimeZone timeZoneWithName:@”Asia/Shanghai”];
2. 如果需要獲取指定時(shí)區(qū)的時(shí)間字符串需要搭配NSDateFormatter來使用。NSDateFormatter可以將NSDate對象轉(zhuǎn)換成所需的日期字符串
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];//分配內(nèi)存,用以存放日期格式
- [formatter setDateFormat:@”yyyy-MM-dd hh-mm-ss”];//定義格式
- NSString * locationString = [formatter stringFromDate:[NSDate date]];//日期輸出出來,用字符串進(jìn)行接收
3.使用NSDateFormatter可以將字符串轉(zhuǎn)換成NSDate類型。同樣需要注意格式的問題。
- NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
- NSString * dateStr = @”2013-04-25 16:23:55”;
- NSDate * date = [formatter dateFromString:dateStr];//把字符串轉(zhuǎn)換成Date格式
最后,不能為任意日期格式的字符串創(chuàng)建NSDateFormatter對象。
四、NSTimer
1. NSTimer是Cocoa中比較常用的定時(shí)器類,它可以完成定時(shí)功能。使用NSTimer需要記住三要素:
---時(shí)間間隔NSTimeInterval為浮點(diǎn)型
---事件代理delegate
---事件處理方法@selector
2.常使用+scheduledTimerWithTimeInterval: target: selector: userInfo: repeat: 方法創(chuàng)建
參數(shù)說明:
--Interval設(shè)定x秒后啟動(dòng)定時(shí)器;
--target參數(shù)是指執(zhí)行第三個(gè)參數(shù)方法的對象
--selector指定了定時(shí)器觸發(fā)調(diào)用的方法;
--userInfo指定了定時(shí)器的用戶信息,可以設(shè)置成nil,也可以通過這個(gè)參數(shù)傳值
--repeat參數(shù)如果設(shè)置成YES,定時(shí)器將會(huì)按照預(yù)定的時(shí)間重復(fù)執(zhí)行,如果設(shè)置成NO,定時(shí)器對象啟動(dòng)一次后不再重復(fù)執(zhí)行。
NSTimer的使用示例
- NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval: 1.0 target : self selector:@selector(showTime:)useInfo:nil repeat:NO];
- -(void)showTime:(NSTimer *)theTimer{
- NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
- NSString * date = [formatter stringFromDate:[NSDate date]];
- if([date isEqualToString:@”2013-04-25 16:46:10”])
- [timer invalidate];
- NSLog(@”date:%@”,date);
- }