NSDate + Change .h文件


#import<Foundation/Foundation.h>@interfaceNSData(Change)+(NSDate*)change;+(NSDate*)getDateWithDateString:(NSString*)strDate;@end

NSDate + Change .m文件

#import"NSData+Change.h"@implementationNSData(Change)+(NSDate*)change{NSString*str=@"20140402142850";NSDateFormatter*formatter=[[NSDateFormatteralloc]init];[formattersetDateFormat:@"YYYYMMddHHmmss"];//NSTimeZone*timeZone=[NSTimeZonetimeZoneWithAbbreviation:@"GMT"];[formattersetTimeZone:[NSTimeZonetimeZoneWithAbbreviation:@"GMT"]];//上面两行是设置时区加上八个小时NSDate*dateTime=[formatterdateFromString:str];returndateTime;}+(NSDate*)getDateWithDateString:(NSString*)strDate{//NSDateFormatter*formatter=[[NSDateFormatteralloc]init];//[formattersetDateFormat:@"YYYYMMddHHmmss"];//NSTimeZone*timeZone=[NSTimeZonetimeZoneWithAbbreviation:@"GMT"];//[formattersetTimeZone:timeZone];//上面两行是设置时区加上八个小时//NSDate*dateTime=[formatterdateFromString:strDate];//returndateTime;NSDateFormatter*formatter=[[NSDateFormatteralloc]init];[formattersetDateFormat:@"YYYYMMddHHmmss"];NSTimeZone*timeZone=[NSTimeZonetimeZoneWithAbbreviation:@"GMT"];[formattersetTimeZone:timeZone];NSDate*dateTime=[formatterdateFromString:strDate];returndateTime;}@end


main.m中代码

NSDate*a=[NSDatachange];NSLog(@"%@",a);NSDate*aa=[NSDatagetDateWithDateString:@"20140402142850"];NSLog(@"%@",aa);/*1.Category定义的方法优先级比本类中的方法高2.Category只能定义方法,不能勋仔实例变量3.Category定义的方法如果和本类中的法方重名,会出现警告。*/

//1.利用NSDateFormatter将日期按照一定格式输出:转换成字符串/*NSDate*date=[NSDatedate];NSDateFormatter*fra=[[NSDateFormatteralloc]init];[frasetDateFormat:@"yyyy-MM-ddhh:mm:ss"];NSString*dateStr=[frastringFromDate:date];NSLog(@"%@",dateStr);*///2.将格式串对象按照NSDate对象输出/*//创建字符串NSString*dateStr=@"2008-08-0820:08:08";//创建格式化对象NSDateFormatter*fra=[[NSDateFormatteralloc]init];//设置输出格式[frasetDateFormat:@"yyyy-MM-ddHH:mm:ss"];//将格式穿对象转换成NSDate对象NSDate*date1=[fradateFromString:dateStr];NSLog(@"%@",date1);*/