.h文件
//
// ZFSDateFormatUtil.h
// XCUtil
//
// Created by 赵发生 on 16/7/23.
// Copyright (c) 2016年 赵发生. All rights reserved.
//
/**************************************************************
*
* 对常用的时间格式进行简化 宜租车专属(赵发生)
*
**************************************************************/
#import <Foundation/Foundation.h>
@interface ZFSDateFormatUtil : NSObject
/**
根据阳历获取中国农历时间
@param date 阳历时间
@return 中国农历时间
*/
+(NSString*)getChineseCalendarWithDate:(NSString*)date;
/**
* 传入距1970年1月1日 多少年 多少天 返回当月的总天数
*/
+ (NSInteger)getDayCountYear:(NSInteger)year Month:(NSInteger)month;
/**
获取系统时间
@return 返回值
*/
+(NSArray*)getsystemtime;
/**
当前书剑管理
@return 返回值
*/
+ (NSDateComponents *)nowDateCmp;
/**
根据字符串获取date数据
@param time 时间字符串
@param formatStr 格式
@return 返回值
*/
+ (NSDate *)getDateWithTime:(NSString *)time Formatter:(NSString *)formatStr;
/**
* 时间转换到字符串
*
* @param style 格式化样式
* @param date 格式化的时间
*
* @return 格式化后的时间字符串
*/
+ (NSString *)dateFormatToStringWithFormatStyle:(NSString *)style date:(NSDate *)date;
/**
* 字符串转换到时间
*
* @param style 格式化样式
* @param dateString 字符串时间
*
* @return 时间对象
*/
+ (NSDate *)stringFormatToDateWithFormatStyle:(NSString *)style string:(NSString *)dateString;
/**
* 时间戳转时间字符串(yyyy-MM-dd HH:mm:ss)
*
* @param interval 时间戳
*
* @return 时间字符串
*/
+ (NSString *)dateFullStringWithInterval:(double)interval;
/**
* 时间戳转时间字符串
*
* @param interval 时间戳
* @param style 格式化的格式
*
* @return 时间字符串
*/
+ (NSString *)dateFullStringWithInterval:(double)interval formatStyle:(NSString *)style;
/**
* 时间字符串(yyyy-MM-dd HH:mm:ss)转时间戳
*
* @param dateString 时间字符串
*
* @return 时间戳
*/
+ (NSTimeInterval)timeIntervalWithDateString:(NSString *)dateString;
/**
* 时间对象转成成最早的时间字符串(yyyy-MM-dd 00:00:00)
*
* @param date 时间对象
*
* @return 时间字符串
*/
+ (NSString *)earliestDateStringWithDate:(NSDate *)date;
/**
* 时间对象转成成最晚的时间字符串(yyyy-MM-dd 23:59:59)
*
* @param date 时间对象
*
* @return 时间字符串
*/
+ (NSString *)latestDateStringWithDate:(NSDate *)date;
/**
* 时间对象转换成日期字符串(yyyy-MM-dd)
*
* @param date 时间对象
*
* @return 日期字符串
*/
+ (NSString *)shortDateString:(NSDate *)date;
@end
.m文件
//
// XCDateFormatUtil.m
// XCUtil
//
// Created by 赵发生 on 16/7/23.
// Copyright (c) 2016年 赵发生. All rights reserved.
//
#import "ZFSDateFormatUtil.h"
@implementation ZFSDateFormatUtil
static NSDateFormatter *formatter = nil;
+ (void)initialize
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [[NSDateFormatter alloc] init];
NSTimeZone * zone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:zone];
});
}
+(NSString*)getChineseCalendarWithDate:(NSString*)date{
NSArray *chineseYears = [NSArray arrayWithObjects:
@"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉",
@"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛己", @"壬午", @"癸未",
@"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳",
@"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸丑",
@"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑",
@"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥", nil];
NSArray *chineseMonths=[NSArray arrayWithObjects:
@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",
@"九月", @"十月", @"冬月", @"腊月", nil];
NSArray *chineseDays=[NSArray arrayWithObjects:
@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];
// [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSDate *dateTemp = nil;
NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
[dateFormater setDateFormat:@"yyyy-MM-dd"];
dateTemp = [dateFormater dateFromString:date];
NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:dateTemp];
NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1];
NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];
NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];
NSString *chineseCal_str =[NSString stringWithFormat: @"%@_%@_%@",y_str,m_str,d_str];
return chineseCal_str;
}
+ (NSInteger)getDayCountYear:(NSInteger)year Month:(NSInteger)month
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date1970 = [NSDate dateWithTimeIntervalSince1970:8*3600];
NSDate *date = [calendar dateByAddingUnit:NSCalendarUnitYear value:year - 1970 toDate:date1970 options:NSCalendarMatchFirst];
date = [calendar dateByAddingUnit:NSCalendarUnitMonth value:month-1 toDate:date options:NSCalendarMatchFirst];
NSInteger day = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
return day;
}
+(NSArray*)getsystemtime{
NSDate *date = [NSDate date];
NSTimeInterval sec = [date timeIntervalSinceNow];
NSDate *currentDate = [[NSDate alloc]initWithTimeIntervalSinceNow:sec];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd"];
NSString *na = [df stringFromDate:currentDate];
return [na componentsSeparatedByString:@"-"];
}
+ (NSDateComponents *)nowDateCmp
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute;
NSDateComponents *cmp = [calendar components:unitFlags fromDate:[NSDate date]];
return cmp;
}
+ (NSDate *)getDateWithTime:(NSString *)time Formatter:(NSString *)formatStr
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:formatStr];
NSDate *date = [formatter dateFromString:time];
return date;
}
+ (NSString *)dateFormatToStringWithFormatStyle:(NSString *)style date:(NSDate *)date
{
[formatter setDateFormat:style];
return [formatter stringFromDate:date];
}
+ (NSDate *)stringFormatToDateWithFormatStyle:(NSString *)style string:(NSString *)dateString
{
[formatter setDateFormat:style];
return [formatter dateFromString:dateString];
}
+ (NSString *)dateFullStringWithInterval:(double)interval
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
return [self dateFormatToStringWithFormatStyle:@"yyyy-MM-dd HH:mm:ss" date:date];
}
+ (NSString *)dateFullStringWithInterval:(double)interval formatStyle:(NSString *)style
{
if (interval > 1400000000000) {
interval /= 1000;
}
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
return [self dateFormatToStringWithFormatStyle:style date:date];
}
+ (NSTimeInterval)timeIntervalWithDateString:(NSString *)dateString
{
return [[self stringFormatToDateWithFormatStyle:@"yyyy-MM-dd HH:mm:ss" string:dateString] timeIntervalSince1970];
}
+ (NSString *)earliestDateStringWithDate:(NSDate *)date
{
return [self dateFormatToStringWithFormatStyle:@"yyyy-MM-dd 00:00:00" date:date];
}
+ (NSString *)latestDateStringWithDate:(NSDate *)date
{
return [self dateFormatToStringWithFormatStyle:@"yyyy-MM-dd 23:59:59" date:date];
}
+ (NSString *)shortDateString:(NSDate *)date
{
return [self dateFormatToStringWithFormatStyle:@"yyyy-MM-dd" date:date];
}
@end