免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 933 | 回复: 1
打印 上一主题 下一主题

Category使用实例,格式化时间 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-11 16:47 |只看该作者 |倒序浏览
Category使用实例,格式化时间





Java代码
  1. 1.//   
  2. 2.//  NSDateCategory.h   
  3. 3.//  WebGameIphone   
  4. 4.//   
  5. 5.//  Created by apple on 12-1-9.   
  6. 6.//  Copyright 2012年 __MyCompanyName__. All rights reserved.   
  7. 7.//   
  8. 8.  
  9. 9.#import <Foundation/Foundation.h>   
  10. 10.  
  11. 11.@interface NSDate (NSDateCategory)   
  12. 12.  
  13. 13.// 根据格式化样式得到时间字符串   
  14. 14.- (NSString *)stringWithFormat:(NSString*)fmt;   
  15. 15.  
  16. 16.// 根据字符串和格式化样式得到时间   
  17. 17.+ (NSDate *)dateFromString:(NSString*)str withFormat:(NSString*)fmt;   
  18. 18.  
  19. 19.// 改变时间字符串格式化样式   
  20. 20.+ (NSString *)dateStringReplaceFormat:(NSString*)str oldFormat:(NSString*)fmt newFormat:(NSString*)n_fmt;   
  21. 21.  
  22. 22.@end  
  23. 23.  
  24. 24.@implementation NSDate (NSDateCategory)   
  25. 25.  
  26. 26.- (NSString *)stringWithFormat:(NSString *)fmt {   
  27. 27.    static NSDateFormatter *fmtter;   
  28. 28.      
  29. 29.    if (fmtter == nil) {   
  30. 30.        fmtter = [[NSDateFormatter alloc] init];   
  31. 31.    }   
  32. 32.      
  33. 33.    if (fmt == nil || [fmt isEqualToString:@""]) {   
  34. 34.        fmt = @"HH:mm:ss";   
  35. 35.    }   
  36. 36.      
  37. 37.    [fmtter setDateFormat:fmt];   
  38. 38.      
  39. 39.    return [fmtter stringFromDate:self];   
  40. 40.}   
  41. 41.  
  42. 42.+ (NSDate *)dateFromString:(NSString *)str withFormat:(NSString *)fmt {   
  43. 43.    static NSDateFormatter *fmtter;   
  44. 44.      
  45. 45.    if (fmtter == nil) {   
  46. 46.        fmtter = [[NSDateFormatter alloc] init];   
  47. 47.    }   
  48. 48.      
  49. 49.    if (fmt == nil || [fmt isEqualToString:@""]) {   
  50. 50.        fmt = @"HH:mm:ss";   
  51. 51.    }   
  52. 52.      
  53. 53.    [fmtter setDateFormat:fmt];   
  54. 54.      
  55. 55.    return [fmtter dateFromString:str];   
  56. 56.}   
  57. 57.  
  58. 58.+ (NSString *)dateStringReplaceFormat:(NSString*)str oldFormat:(NSString*)fmt newFormat:(NSString*)n_fmt {   
  59. 59.    NSDate *n_date = [NSDate dateFromString:str withFormat:fmt];   
  60. 60.    return [n_date stringWithFormat:n_fmt];   
  61. 61.}   
  62. 62.  
  63. 63.@end  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-11 22:32 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP