/**
*NSDate和NSDateComponents
*/
/**
*创建标识今天的日期
*方法:NSDate中的date方法创建表示当前日期的对象
*/
NSDate *todaysDate=[NSDate date];
/**
*获取今天的日期列子
*/
#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSDate *todaysDate=[NSDate date];
NSLog(@"Today‘s date is %@",todaysDate);
}
return 0;
}
//打印消息如下:
Today‘s date is 2012-6-27 13:14:30 +0000
/**
*自定义日期的创建
*/
NSDateComponents:代表构成日期与时间的细节信息:天、月、年与小时。
NSCalendar:标识真实世界的日历,用于刮泥与日历相关的复杂操作
NSDate:基类
NSDateComponents *dateComponents=[[NSDateComponents alloc]init];
dateComponents.year=2007;
dateComponents.month=6;
dateComponents.day=29;
dateComponents.hour=12;
dateComponents.minute=01;
dateComponents.second=31;
dateComponents.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"PDT"];
/**
*DEMO
*
/
#import <Foundation/Foudation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSDateComponents *dateComponents=[[NSDateComponents alloc]init];
dateComponents.year=2007;
dateComponents.month=6;
dateComponents.day=29;
dateComponents.hour=12;
dateComponents.minute=01;
dateComponents.second=31;
dateComponents.timeZone=[NSTimeZone tmeZoneWithAbbreviation:@"PDT"];
NSDate *iPhoneRelaeaseDate=[[NSCalendar currentCalendar]dateFromComponents:dateComponents];
NSLog(@"The original iPone went on sale:%@",iPoneReleaseDate);
}
return 0;
}
//输出信息
The original iPone went on sale:2007-06-29 19:01:31 +0000
/**
*比较;两个日期
*/
//比较相等
NSDate *todaysDate=[NSDate date];
if([todaysDate isEqualToDate:iPhoneReleaseDate]){
NSLog(@"The iPone was released todya!");
}
else {
NSLog(@"The iPone was released on some other date");
}
//判断日期早晚earlierDate
NSDate *earlierDateIs=[todayDate earlierDate:iPoneReleaseDate];
//laterDate
NSDate *laterDateIs=[todaysDate laterDate:iPoneReleaseDate];
//日期秒数之差timeIntervalSinceDate
NSCalendar *systemCalendar=[NSCalendar currentCalendar];
usigined int unitFlags=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
NSDateCompnents *dateComparisonComponents=[systemCalendar components:unitFlags fromDate:iPoneReleaseDate
toDate:todaysDate options:NSWrapCalendarComponents];
#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSDateComponents *dateComponents=[[NSDateComponents alloc]init];
dateComponents.year=2007;
dateComponents.month=6;
dateComponents.day=29;
dateComponents.hour=12;
dateComponents.minute=01;
dateComponents.second=31;
dateComponents.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"PDT"];
NSDate *iPhoneReleaseDate=[[NSCalendar currentCalendar]dateFromComponents:dateComponents];
NSLog(@"The original iPone went on sale:%@",iPoneReleaseDate);
NSDate *todayDate=[NSDate date];
NSLog(@"Today‘s date is:%@",todaysDate);
if([todaysDate isEqualToDate:iPoneReleaseDate]){
NSLog(@"The iPone was released today!");
}
else{
NSLog(@"The iPone was released today!");
}
NSDate *earlierDateIs=[todaysDate earlierDate:iPoneReleaseDate];
NSLog(@"The earlier date id:%@",earlierDateIs);
NSDate *laterDateIs=[todayDate laterDate:iPoneReleaseDate];
NSLog(@"The later date is:%@",laterDateIs);
NSTimeInterval timeBetweenDates=[todaysDate timeIntervalSinceDate:iPhoneReleaseDate];
NSLog(@"The iPone was release was released %f seconds ago",timeBetweenDates);
NSCalendar *systemCalendar =[NSCalendar currentCalendar];
unsigned int uniFlags=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSDayCalendarUnit;
NSDateComponents *dateComparisonComponents = [systemCalendar components:unitFlags fromDate:iPoneReleaseDate toDate:todaysDate options:NSWrapCalendarComponents];
NSLog(@"The iPone was released %ld years,%ld months and %ld days ago",dateComparisonComponents.year,dateComparisonComponents.month,dateComparisonComponents.day);
}
return 0;
}
/**
*将字符串转换为日期
*/
//NSDateFormatter
NSString *[email protected]"02/14/2012"
NSDateFormatter *df=[[NSDateFormatter alloc]init];//创建格式化类对象
[email protected]"MM/dd/yyyy";//设置字符串格式
NSDate *valentinesDay=[df dateFromString:dateString];//执行转换字符串函数将字符串转换为date类型
/**
*日期转换例子
*/
import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepooll{
NSString *[email protected]"02/14/2012";
NSDateFormatter *df=[[NSDateFormatter alloc]init];
[email protected]"MM/dd/yyyy";
NSDate *valentinesDay=[df dateFromString:dateString];
NSLog(@"Valentine‘s Day =%@",valentinesDay);
}
return 0;
}
//打印信息:
Valentine‘s Day =2012-02-14 05:02:00 +0000
//格式化日期以便显示:
[email protected]"EEEE,MMMM d";
/**
*格式化日期例子
*/
#import <Foundation/Foundation.h>
int main(int argc,const char *argv[]){
@autoreleasepool{
NSString *[email protected]"02/14/2012";
NSDateFormatter *df=[[NSDateFormatter alloc]init];
[email protected]"MM/dd/yyyy";
NSDate *valentinesDay=[df dateFromStrign:dateString];
NSLog(@"Unformatted Valentine‘s Day=%@",valentinesDay);
NSLog(@"Formatted Valendtine‘s Day=%@",[df stringFormDate:valentinesDay]);
}
return 0;
}
/**
*加减日期
*/
//NSDateComponents、NSCalendar NSDateComponents指定了时间长度
NSDateComponents *weekBeforeDateComponents=[[NSDateComponents alloc]init];
//减掉一周就将NSDateComponents中的week属性设为-1
weekBeforeDateComponents.week=-1;
NSDate *vDayShoppingDay=[[NSCalendar currentCalendar]
dateByAddingComponents:weekBeforeDateComponents toDate:valentinesDay options:0];
/**
*加减日期例子
*/
#import <Foundation/Foundation.h>
int main(int argc,const char *arv[]){
@autoreleasepool{
NSString *[email protected]"02/14/2012";
NSDateFormat *df=[[NSDateFormatter alloc[init];
df.dateFormat [email protected]"MM/dd/yyyy";
NSDate *valentineDay=[df dateFromString:dateString];
NSLog(@"valentines‘s Day=%@",valentinesDay);
NSDateComponents *weekBeforeDateComponents=[[NSDateComponents alloc]init];
weekBeforeDateComponents.week=-1;
NSDate *vDayShopingDay=[[NSCalendar currentCalendar] dateByAddingComponetns:weekBeforeDateComponents toDate:valentinesDay optioins:0];
NSLog(@"Shop for Valentine‘s Day by %@",vDayShoppingDay);
}
return 0;
}
/**
*使用定时器调度并重复执行任务
*/
//特定时间重复执行任务方式:NSTimer
//设置时间间隔:dateWithTimeIntervalSinceNow:10.0
NSDate *scheduledTime=[NSDate dateWithTimeINtervalSinceNow:10.0];
NSString *[email protected]"To demo userInfo";
NSTimer *timer=[[NSTimer alloc]initWithFireDate:scheduledTime
interval:2
target:self
selector:@selector(task)
userInfo:customUserObject
repeats:YES];
//参数说明:
参数一:日期对象指定了定时器何时变成活动状态。
参数二:接下来是间隔时间,是定时器再次发送消息前所需等待的秒数。
参数三:是目标参数描述符目标是方法所处的对象由于方法与定时器一样都位于月应用委托中,因此可以使用self关键字
参数四:selector参数是位于圆括号中的方法名前面是@selector关键字。
参数五:userInfo是定时器使用的自定义内容。可以使用任何对象,并且可以获得正在执行的消息中的对象引用(上面的selector参数)。这里使用字符串,但通常会使用字典或其他集合以支持更加复杂的活动。
参数六:repeats参数表示定时器是发送一次消息,还是根据第2个参数指定的时间间隔重复发送。
NSRunLoop *runLoop=[NSRunLoop currentRunLoop[;
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
10秒钟后,定时器将会开始每隔两秒钟向应用发送task消息。
[timer invalidate];
/**
*定时器小例子
*/
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window=_window;
-(void)applicationDidFinishLaunching:(NSNotification *)aNotification{
NSDate *scheduledTime=[NSDate dateWithTimeIntervalSinceNow:10.0];
NSString *[email protected]"To demo userInfo";
NSTimer *timer=[[NSTimer alloc]initWithFireDate:scheduledTime
interval:2
target:self
selector:@selector(task)
userInfo:customUserObject
repeats:YES];
NSRunLoop *runLoop=[NSRunLoop currentRunLoop[;
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(void)task:(id)sender{
NSTimer *localTimer=(NSTimer *)sender;
NSLog(@"Schedule task has executed with this user info :%@",[localTimer userInfo]);
}
@end