Objective-C中的日期格式器NSDateFormatter

来源:互联网 发布:uglify java 编辑:程序博客网 时间:2024/05/16 17:35

NSDateFormatter代表一个日期格式器,它的功能就是完成NSDate与NSString之间的转换。

使用NSDateFormatter完成NSDate与NSString之间转换的步骤如下:

1. 创建一个NSDateFormatter对象

2. 调用NSDateFormatter的setDateStyle:,setTimeStyle:方法设置格式化日期,时间的风格。

其中,日期,时间风格支持如下几个枚举值。

NSDateFormatterNoStyle:不显示日期,时间的风格。

NSDateFormatterShortStyle:显示短的日期,时间风格

NSDateFormatterMediumStyle:显示中等的日期,时间风格

NSDateFormatterLongStyle:显示长的日期,时间风格

NSDateFormatterFullStyle:显示完整的日期,时间风格

如果打算使用自定义的格式模版,可以调用NSDateFormatter的setDateFormatter:方法设置日期,时间的模版即可。

3.如果需要将NSDate转换为NSString,调用NSDateFormatter的stringFromDate:方法执行格式化即可;

如果需要将NSString转换为NSDate,调用NSDateFormatter的dateFromString:方法执行格式化即可。

如下程序示范来NSDateFormatter的功能和用法.

[objc] view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. int main(int argc, const charchar * argv[])  
  4. {  
  5.   
  6.     @autoreleasepool {  
  7.           
  8.         //需要被格式化的时间为从1970年1.1开始,20年后的日期  
  9.         NSDate* dt = [NSDate dateWithTimeIntervalSince1970:20*366*24*3600];  
  10.         //创建两个NSLocale,分别代表中国,美国  
  11.         NSLocale* locales[] = {[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"],  
  12.                                 [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]};  
  13.         NSDateFormatter* df[8];  
  14.         //为上面两个NSLocale创建8个DateFormat对象  
  15.         for (int i=0; i<2; i++) {  
  16.             df[i * 4] = [[NSDateFormatter alloc] init];  
  17.             //设置NSDateFormatter的日期,时间风格  
  18.             [df[i * 4] setDateStyle:NSDateFormatterShortStyle];  
  19.             [df[i * 4] setTimeStyle:NSDateFormatterShortStyle];  
  20.             //设置NSDateFormatter的NSLocale  
  21.             [df[i * 4] setLocale:locales[i]];  
  22.               
  23.             df[i * 4 + 1] = [[NSDateFormatter alloc] init];  
  24.             //设置NSDateFormatter的日期,时间风格  
  25.             [df[i * 4 + 1] setDateStyle:NSDateFormatterMediumStyle];  
  26.             [df[i * 4 + 1] setTimeStyle:NSDateFormatterMediumStyle];  
  27.             //设置NSDateFormatter的NSLocale  
  28.             [df[i * 4 + 1] setLocale:locales[i]];  
  29.               
  30.             df[i * 4 + 2] = [[NSDateFormatter alloc] init];  
  31.             //设置NSDateFormatter的日期,时间风格  
  32.             [df[i * 4 + 2] setDateStyle:NSDateFormatterLongStyle];  
  33.             [df[i * 4 + 2] setTimeStyle:NSDateFormatterLongStyle];  
  34.             //设置NSDateFormatter的NSLocale  
  35.             [df[i * 4 + 2] setLocale:locales[i]];  
  36.               
  37.             df[i * 4 + 3] = [[NSDateFormatter alloc] init];  
  38.             //设置NSDateFormatter的日期,时间风格  
  39.             [df[i * 4 + 3] setDateStyle:NSDateFormatterFullStyle];  
  40.             [df[i * 4 + 3] setTimeStyle:NSDateFormatterFullStyle];  
  41.             //设置NSDateFormatter的NSLocale  
  42.             [df[i * 4 + 3] setLocale:locales[i]];  
  43.         }  
  44.           
  45.         for (int i=0; i<2; i++) {  
  46.             switch (i) {  
  47.                 case 0:  
  48.                     NSLog(@"----------中国日期格式----------");  
  49.                     break;  
  50.                 case 1:  
  51.                     NSLog(@"----------美国日期格式----------");  
  52.                     break;  
  53.                   
  54.             }  
  55.             NSLog(@"SHORT格式的日期格式:%@",[df[i * 4] stringFromDate:dt]);  
  56.             NSLog(@"MEDIUM格式的日期格式:%@",[df[i * 4 + 1] stringFromDate:dt]);  
  57.             NSLog(@"LONG格式的日期格式:%@",[df[i * 4 + 2] stringFromDate:dt]);  
  58.             NSLog(@"FULL格式的日期格式:%@",[df[i * 4 + 3] stringFromDate:dt]);  
  59.         }  
  60.           
  61.         NSDateFormatter* df2 = [[NSDateFormatter alloc] init];  
  62.         //设置自定义的格式器模版  
  63.         [df2 setDateFormat:@"公元yyyy年MM月DD日 HH时mm分"];  
  64.         //执行格式化  
  65.         NSLog(@"%@",[df2 stringFromDate:dt]);  
  66.           
  67.         NSString* dateStr = @"2013-03-12";  
  68.         NSDateFormatter* df3 = [[NSDateFormatter alloc] init];  
  69.         //根据日期字符串的格式设置格式模版  
  70.         [df3 setDateFormat:@"yyyy-MM-dd"];  
  71.         //将字符串转换为NSDate对象  
  72.         NSDate* date2 = [df3 dateFromString:dateStr];  
  73.         NSLog(@"%@",date2);  
  74.           
  75.           
  76.           
  77.           
  78.           
  79.     }  
  80.     return 0;  
  81. }  

其打印日志如下:

2014-08-26 22:36:42.465 NSDateFormatterTest[466:303] ----------中国日期格式----------

2014-08-26 22:36:42.467 NSDateFormatterTest[466:303] SHORT格式的日期格式:90-1-16 上午8:00

2014-08-26 22:36:42.467 NSDateFormatterTest[466:303] MEDIUM格式的日期格式:1990116 上午8:00:00

2014-08-26 22:36:42.468 NSDateFormatterTest[466:303] LONG格式的日期格式:1990116 GMT+8上午8:00:00

2014-08-26 22:36:42.469 NSDateFormatterTest[466:303] FULL格式的日期格式:1990116 星期二 中国标准时间上午8:00:00

2014-08-26 22:36:42.469 NSDateFormatterTest[466:303] ----------美国日期格式----------

2014-08-26 22:36:42.470 NSDateFormatterTest[466:303] SHORT格式的日期格式:1/16/90, 8:00 AM

2014-08-26 22:36:42.470 NSDateFormatterTest[466:303] MEDIUM格式的日期格式:Jan 16, 1990, 8:00:00 AM

2014-08-26 22:36:42.471 NSDateFormatterTest[466:303] LONG格式的日期格式:January 16, 1990 at 8:00:00 AM GMT+8

2014-08-26 22:36:42.471 NSDateFormatterTest[466:303] FULL格式的日期格式:Tuesday, January 16, 1990 at 8:00:00 AM China Standard Time

2014-08-26 22:36:42.475 NSDateFormatterTest[466:303] 公元19900116 0800

2014-08-26 22:36:42.476 NSDateFormatterTest[466:303] 2013-03-11 16:00:00 +0000

Program ended with exit code: 0


原创粉丝点击