object-c 日期总结

/**

*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

时间: 2024-11-07 09:36:07

object-c 日期总结的相关文章

DBCP object created 日期 by the following code was never closed:

1.分析 看到标题 DBCP 首先想到的肯定是 数据库连接池哪方面有问题,那么先别着急去解决,不要一股脑就钻进逻辑代码中,然后启用调试就开始一步一步 的分析.我们首先要做的就是想,想想数据库连接池,在项目中是如何实现的,那么不管你或早或晚,都会想到数据库连接池相关的配置文件和代码.那么 问题来了,是否是配置文件配置的有问题(挖掘技术哪家强?)? 那么先来分析下各项连接池配置的属性: #回收被遗弃的(一般是忘了释放的)数据库连接到连接池中. dataBase.removeAbandoned =fa

Object.prototype.toString.call()方法浅谈

参考链接:http://www.cnblogs.com/wyaocn/p/5796142.html 使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Object.prototype.toString.call(null);//"[object Null]" Object.prototype.toString.call(undefined);/

Object.prototype.toString.call()方法

使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Object.prototype.toString.call(null);//"[object Null]" Object.prototype.toString.call(undefined);//"[object Undefined]" Object.prototype.t

springMvc接受日期类型参数处理

这个问题,也即是springMvc如何进行参数类型的转换 以把client传过来一个String类型,转换为日期类型为例: 1.controller /** * 接收日期类型参数 * 注意: * springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误 * 解决办法 * 1.全局日期处理 * */ @RequestMapping("/test") public String test(Date birthday){ System.out.println(bir

javascript对象和日期

javascript对象有: 1.对象,object; 2.日期,Date; 3.数组.Array; 4.正则表达式,Regex 5.函数 function 对象的定义: 如: var book={}   定义一个空对象: var book2={key:value}; hashMap var book3={"name":"javascript进价","price":10,"date":"2014-7-19"

JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈

toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转化为string类型: console.log(true.toString());//"true" console.log(false.toString());//"false&quo

Object.prototype.toString.call()

使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Object.prototype.toString.call(null);//”[object Null]” Object.prototype.toString.call(undefined);//”[object Undefined]” Object.prototype.toString.call(“a

struts2 数据转换器

四.数据类型的转换(明白原理,实际开发中几乎不用) 1.开发中的情况: 实际开发中用户通过浏览器输入的数据都是String或者String[]. String/String[]----填充模型(set方法)---->POJO(plain old java object)  pojo中有java的数据类型. POJO--------获取(get方法)---->页面展示:String 2.类型转换情况 写数据:(增,删,改)都是String或String[]数组转换为其他类型. 读数据:(查)其他

Javascript第二天

一.数据类型 1.字符串(String) var mood = “happy”; var mood = ‘happy’; 2.数值(number) var age = 25; var price = 33.25; 3.布尔类型 (boolean) 布尔型数据只能有两种种值 true 和 false; var married = true; var married = false; 与字符串不同,不要把布尔值用引号括起来.布尔值 false 与 字符串 "false"是两回事. 4.对象

《JavaScript设计模式与开发》笔记 3.call和apply

1.改变this指向 2.Function.prototype.bind 3.借用其他对象方法 1.借用实现继承 2.实现恶心的 Array.prototype.push.call Array.prototype.join.call Array.prototype.slice.call Object.prototype.toString.call 1.改变this指向 var obj1 = { name:"Bob marley" }; var obj2 = { name:"B