zoukankan      html  css  js  c++  java
  • IOS 公共类-MyDateUtil 日期处理Util

    IOS 公共类-MyDateUtil 日期处理Util

     

    此为处理日期的公共类。适用IOS6+

    .h文件:

     1 #import <Foundation/Foundation.h>
     2 //适用 IOS6+
     3 @interface MyDateUtil
     4 
     5 //返回一个新的日期,新的日期将会只保留年月日
     6 +(NSDate*) keepYearMonthDayWithDate:(NSDate*)oldDate;
     7 //返回日期的年份
     8 +(int)getYearWithDate:(NSDate *)date;
     9 //返回日期的月份
    10 +(int) getMonthWithDate:(NSDate*) date;
    11 //返回日期的日份
    12 +(int) getDayWithDate:(NSDate*) date;
    13 //传入年月日,返回日期类型
    14 +(NSDate*) getDateFromYear:(int)year andMonth:(int)month andDay:(int)day;
    15 //传入一个日期的字符串,一个日期字符串的分隔符,返回日期。 默认为"/",即传入"01/05/2012"和"/",返回日期类型。 "dd/MM/yyyy" "01/05/2012"
    16 +(NSDate*) getDateFromText:(NSString*)dateStr andDateSplit:(NSString*)split;

    .m文件:

     1 #import <Foundation/Foundation.h>
     2 
     3 @implementation MyDateUtil:NSObject
     4 
     5 +(NSDate*) keepYearMonthDayWithDate:(NSDate*)oldDate {
     6     NSCalendar *oldCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
     7     NSDateComponents *oldDateComponents = [oldCal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:oldDate];
     8     
     9     NSDate *result = [oldCal dateFromComponents:oldDateComponents];
    10     return result;
    11 }
    12 
    13 +(int)getYearWithDate:(NSDate *)date {
    14     NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    15     NSDateComponents *dateComp = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
    16     return dateComp.year;
    17 }
    18 
    19 +(int) getMonthWithDate:(NSDate*) date {
    20     NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    21     NSDateComponents *dateComp = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
    22     return dateComp.month;
    23 }
    24 
    25 +(int) getDayWithDate:(NSDate*) date {
    26     NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    27     NSDateComponents *dateComp = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
    28     return dateComp.day;
    29 }
    30 
    31 +(NSDate*) getDateFromYear:(int)year andMonth:(int)month andDay:(int)day {
    32     NSDateComponents *dateComp = [[NSDateComponents alloc] init];
    33     dateComp.year = year;
    34     dateComp.month = month;
    35     dateComp.day = day;
    36     NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    37     NSDate *d = [cal dateFromComponents:dateComp];
    38     return d;
    39 }
    40 
    41 //datestr must like: "dd/MM/yyyy" "01/05/2012"
    42 +(NSDate*) getDateFromText:(NSString*)dateStr andDateSplit:(NSString*)split{
    43     if (!split) {
    44         split = @"/";
    45     }
    46     NSArray *dateStrArr = [dateStr componentsSeparatedByString:@"/"];
    47     return [self getDateFromYear:[[dateStrArr objectAtIndex:2] intValue] andMonth:[[dateStrArr objectAtIndex:1] intValue] andDay:[[dateStrArr objectAtIndex:0] intValue]];
    48 }
    49 
    50 @end

    若要下载,请访问github:https://github.com/cjt321/MyDateUtil

    此处保持最新更新

  • 相关阅读:
    分布式框架服务调用超时设计与实现
    分布式框架灰度发布实现
    写给自己的 SOA 和 RPC 理解
    Java 基础【19】代理
    Java 基础【18】 反射与内省
    Spring HttpInvoker 从实战到源码追溯
    Lombok 使用攻略
    Guava Preconditions 工具参数前置校验
    CAS 单点登录【2】自定义用户验证
    Hibernate 离线对象构建通用查询
  • 原文地址:https://www.cnblogs.com/alunchen/p/5296771.html
Copyright © 2011-2022 走看看