博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
封装用于解析NSDate的便利的类
阅读量:6984 次
发布时间:2019-06-27

本文共 3806 字,大约阅读时间需要 12 分钟。

封装用于解析NSDate的便利的类

此类可以从NSDate中解析出年份,月份,日期,时,分,秒,毫秒,足够用来做好多事情了,现提供源码如下:

以下是核心的类:

TimeInfo.h 与 TimeInfo.m

////  TimeInfo.h//  ShowTime////  Created by YouXianMing on 14-10-16.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import 
@class HumanTimeInfo;@interface TimeInfo : NSObject+ (HumanTimeInfo *)humanCanUnderstandFromDate:(NSDate *)date;@end
////  TimeInfo.m//  ShowTime////  Created by YouXianMing on 14-10-16.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "TimeInfo.h"#import "HumanTimeInfo.h"static NSDateFormatter   *_dateFormatter;@implementation TimeInfo+ (void)initialize {    if (self == [TimeInfo class]) {        _dateFormatter            = [[NSDateFormatter alloc] init];        _dateFormatter.locale     = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];        _dateFormatter.timeZone   = [NSTimeZone timeZoneWithName:@"GMT"];        _dateFormatter.dateFormat = @"yyyy:MM:dd:MMM:MMMM:HH:mm:ss:aa:EEE:EEEE:SSS"; // SSSS    }}+ (HumanTimeInfo *)humanCanUnderstandFromDate:(NSDate *)date {    if (date != nil) {        NSArray *timeInfoArray = [[_dateFormatter stringFromDate:date] componentsSeparatedByString:@":"];        HumanTimeInfo *info    = [HumanTimeInfo new];        info.year              = timeInfoArray[0];        info.mounth            = timeInfoArray[1];        info.day               = timeInfoArray[2];        info.enMounth          = timeInfoArray[3];        info.fullEnMounth      = timeInfoArray[4];        info.hour              = timeInfoArray[5];        info.min               = timeInfoArray[6];        info.sec               = timeInfoArray[7];        info.amPm              = timeInfoArray[8];        info.enWeakday         = timeInfoArray[9];        info.fullWeakday       = timeInfoArray[10];        info.mSec              = timeInfoArray[11];                return info;    } else {        return nil;    }}@end

人类能够理解的信息类:

HumanTimeInfo.h 与 HumanTimeInfo.m

////  HumanTimeInfo.h//  ShowTime////  Created by YouXianMing on 14-10-16.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import 
@interface HumanTimeInfo : NSObject@property (nonatomic, strong) NSString *year; // 2014@property (nonatomic, strong) NSString *mounth; // 10@property (nonatomic, strong) NSString *day; // 16@property (nonatomic, strong) NSString *enMounth; // Oct@property (nonatomic, strong) NSString *fullEnMounth; // October@property (nonatomic, strong) NSString *hour;@property (nonatomic, strong) NSString *min;@property (nonatomic, strong) NSString *sec;@property (nonatomic, strong) NSString *amPm; // 上午或者下午@property (nonatomic, strong) NSString *enWeakday;@property (nonatomic, strong) NSString *fullWeakday;@property (nonatomic, strong) NSString *mSec; // 毫秒@end
////  HumanTimeInfo.m//  ShowTime////  Created by YouXianMing on 14-10-16.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "HumanTimeInfo.h"@implementation HumanTimeInfo@end
NSDate+CurrentTime.h 与 NSDate+CurrentTime.m
////  NSDate+CurrentTime.h//  ShowTime////  Created by YouXianMing on 14-10-16.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import 
@class HumanTimeInfo;@interface NSDate (CurrentTime)+ (HumanTimeInfo *)currentTime;+ (HumanTimeInfo *)dateFrom:(NSDate *)date;@end
////  NSDate+CurrentTime.m//  ShowTime////  Created by YouXianMing on 14-10-16.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import "NSDate+CurrentTime.h"#import "HumanTimeInfo.h"#import "TimeInfo.h"@implementation NSDate (CurrentTime)+ (HumanTimeInfo *)currentTime{    return [TimeInfo humanCanUnderstandFromDate:[NSDate date]];}+ (HumanTimeInfo *)dateFrom:(NSDate *)date{    return [TimeInfo humanCanUnderstandFromDate:date];}@end

使用的话,给出NSDate,然后解析出来,就是这么简单:)

转载地址:http://gaxpl.baihongyu.com/

你可能感兴趣的文章
《面向对象分析与设计》一3.2 参与者
查看>>
WCF 性能基准报告
查看>>
智迪科技携手海通安恒,启动SAP实施项目
查看>>
机器学习算法在自动驾驶领域的应用大盘点!
查看>>
《深入理解Android:Telephony原理剖析与最佳实践》一1.1 智能手机的系统结构
查看>>
卡斯特罗的离去对古巴科技产业的未来有何影响?
查看>>
维护网络安全要攻防兼备
查看>>
美国第一大移动运营商的5G战略:已进入预商用测试
查看>>
“物联网+云平台”的实验室管理方案,瞄准的是生物医药和化工行业
查看>>
OA系统选型分析之致远OA与华天动力OA
查看>>
联想确认再次裁员 称调整主要分布在海外
查看>>
大鱼吃光小鱼,绝不可能!盘点2016存储行业发生的大事件
查看>>
人中急救穴 也可通过辨别疾病
查看>>
2020年全球云服务规模将达3900亿美元
查看>>
Facebook、Netflix 等多家科技巨头谈“设计”
查看>>
雅虎核心业务售与Verizon:互联网先驱的时代终结
查看>>
市场规模占全国4成,广东物联网市场发展强劲
查看>>
ICS—CERT官网公示匡恩网络新发现四工控漏洞
查看>>
英国电价与光伏容量占比关系分析
查看>>
浅谈对5G核心网演进方向的几点展望
查看>>