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

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

以下是核心的类:

TimeInfo.h 与 TimeInfo.m

//
// TimeInfo.h
// ShowTime
//
// Created by YouXianMing on 14-10-16.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
@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[];
info.mounth = timeInfoArray[];
info.day = timeInfoArray[];
info.enMounth = timeInfoArray[];
info.fullEnMounth = timeInfoArray[];
info.hour = timeInfoArray[];
info.min = timeInfoArray[];
info.sec = timeInfoArray[];
info.amPm = timeInfoArray[];
info.enWeakday = timeInfoArray[];
info.fullWeakday = timeInfoArray[];
info.mSec = timeInfoArray[]; 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 <Foundation/Foundation.h> @interface HumanTimeInfo : NSObject @property (nonatomic, strong) NSString *year; //
@property (nonatomic, strong) NSString *mounth; //
@property (nonatomic, strong) NSString *day; //
@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 <Foundation/Foundation.h>
@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,然后解析出来,就是这么简单:)

封装用于解析NSDate的便利的类的更多相关文章

  1. Android JSON、GSON、FastJson的封装与解析

    声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...

  2. [知了堂学习笔记]_JSON数据操作第2讲(JSON的封装与解析)

    上一讲为大家讲了什么是JSON,那么这一讲为大家带来了在WEB项目中JSON的用法,也就是JSON的封装与解析. 此图是数据库中的部分内容 一.JSON封装 所谓的JSON封装,指的是在Servlet ...

  3. DOM4J操作XML(包括封装和解析)

    DOM4J操作XML(包括封装和解析) 先来看看dom4j中相应XML的DOM树建立的继承关系 针对于XML标准定义.相应于图2-1列出的内容,dom4j提供了下面实现: 以下给出一个详细事例: pa ...

  4. 090 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 04 使用包进行类管理(2)——导入包

    090 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 ...

  5. jsoncpp封装和解析字符串、数字、布尔值和数组

    使用jsoncpp进行字符串.数字.布尔值和数组的封装与解析. 1)下载jsoncpp的代码库 百度网盘地址 :http://pan.baidu.com/s/1ntqQhIT 2)解压缩文件 json ...

  6. MinerUrl.java 解析页面后存储URL类

    MinerUrl.java 解析页面后存储URL类 package com.iteye.injavawetrust.miner; /** * 解析页面后存储URL类 * @author InJavaW ...

  7. Python中第三方的用于解析HTML的库:BeautifulSoup

    背景 在Python去写爬虫,网页解析等过程中,比如: 如何用Python,C#等语言去实现抓取静态网页+抓取动态网页+模拟登陆网站 常常需要涉及到HTML等网页的解析. 当然,对于简单的HTML中内 ...

  8. python 以单例模式封装logging相关api实现日志打印类

    python 以单例模式封装logging相关api实现日志打印类   by:授客QQ:1033553122 测试环境: Python版本:Python 2.7   实现功能: 支持自由配置,如下lo ...

  9. 视音频数据处理入门:FLV封装格式解析

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

随机推荐

  1. 解决python3与python2的pip命令冲突问题冲突(window版)

    解决方法再上一篇有大概讲解: python开发环境安装配置 这里做一些补充: 上一篇说过,删除python3和python2中的python.exe文件后关闭dos窗口,重新打开dos,就可以进行安装 ...

  2. InterView之PHP(2)

    PHP 理论知识 常用的超全局变量(8个) $_GET ----->get传送方式 $_POST ----->post传送方式 $_REQUEST ----->可以接收到get和po ...

  3. Mac 硬盘中各个文件夹详解

    打开Macintosh HD你会发现内中有四个文件夹(一般情况下,隐藏文件夹是不可见的,而且,可能会更多,比如安装xcode后会有developer文件夹). 分别有——应用程序(Applicatio ...

  4. 数据库应用(Mysql、Mongodb、Redis、Memcached、CouchDB、Cassandra)

    目前,主流数据库包括关系型(SQL)和非关系型(NoSQL)两种. 关系数据库是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据,支持复杂的事物处理和结构化查询.代表实 ...

  5. Automapper问题记录

    在Automapper使用中会碰到一些未能映射或者错误的问题,这些问题可能会经常忘记如何处理,想到一些就记录一些: 映射值有时为空又不报错的情况 这很可能是由于目标类中的部分属性有问题导致的,最简单的 ...

  6. SharePoint 2013创建应用程序时IIS端口文件夹下没文件

    最近SharePoint 2007迁移到2013的时候,碰到创建应用程序时IIS端口文件夹下没文件的问题,网上找了大把的原因,终于在这里找到了解决方案: Fix: 1. Open IIS on the ...

  7. bzoj 3600: 没有人的算术

    Description Solution 我们可以给每一个数钦定一个权值 , 这样就可以 \(O(1)\) 比较大小了. 考虑怎么确定权值: 用平衡树来维护 , 我们假设根节点管辖 \([1,2^{6 ...

  8. DataTable如何删除特定行

    DataTable dt = dataSet1.table1.GetAllRows(); DataRow[] foundRow = dt.Select("catalogid = 0" ...

  9. Vertica备份恢复

    Vertica备份和恢复数据库 Vertica提供了一个功能全面的使用程序--vbr, 他是一个Python脚本.使用vbr脚本可以备份和还原完整备份以及为特定架构或表创建备份.vbr实用程序会在首次 ...

  10. EventLog组件读写事件日志

    使用.Net中的EventLog控件使您可以访问或自定义Windows 事件日志,事件日志记录关于重要的软件或硬件事件的信息.通过 EventLog,可以读取现有日志,向日志中写入项,创建或删除事件源 ...