Foundation框架中的日期操作总结

看官们,我们在前面章回中介绍了Foundation框架中的NSCalendar,本章回中将介绍Foundation框架中的日期操作做总结。


日期操作主要涉及NSDate、NSDateFormatter和NSCalendar三个大类以及NSLocale、NSTimeZone和NSDateComponents三个小类。我们在前面章回中详细介绍过这些类以及类中经常使用的方法,本章回中将从日期操作的角度来对这些类做一个整体的总结:

  • 1.程序中对日期的操作都是通过NSDate类进行的,它是日期操作的核心类;
  • 2.程序中需要直观的字符类型(NSString)时需要使用NSDateFormatter类来转换NSDate对象;
  • 3.NSDateFormatter类可以提供符合程序需求的各种格式的日期,当然要注意地区和时区的差异;
  • 3.NSDateFormatter类中对于地区和时区的差异通过NSLocale和NSTimeZone这两个类的相关操作来完成;
  • 4.程序中对日期和时间单元的操作通过NSCalendar类和它的辅助类NSDateComponents来完成,需要掌握类的中相关方法;


最近几个章回的内容都偏重于理论的,接下来我们通过具体的代码来演示使用操作日期:

#include<Foundation/Foundation.h>

int main() {
    //获取日期和时间
    NSDate *date1 = [NSDate date];
    NSDate *date2 = [[NSDate alloc] init];
    NSDate *date3 = [NSDate dateWithTimeIntervalSinceNow:3600*2];

    NSLog(@"date1: %@",date1);
    NSLog(@"date2: %@",date2);
    NSLog(@"date3: %@",date3);

    //计算日期和时间的差,时间差本质是double类型,单位是秒
    NSLog(@"date3 - date2: %f", [date3 timeIntervalSinceDate:date2]);
    NSLog(@"date2 - date3: %f", [date2 timeIntervalSinceDate:date3]);
    // NSLog(@"China date: %@",[date descriptionWithLocale:@"cn"]);
    // NSLog(@"China date: %@",[date descriptionWithLocale:@"en_US"]);

    //比较日期和时间
    switch([ date1 compare: date3]) {
        case NSOrderedSame:
            NSLog(@" date1 == date3");
        break;
        case NSOrderedDescending:
            NSLog(@" date1 > date3");
        break;
        case NSOrderedAscending:
            NSLog(@" date1 < date3");
        break;
        default:
        break;
    }

    //使用NSDateFormatter把当前地区的日期和时间转换为指定地区和日期和时间(NSDate转换成NSString)
    //注意步骤:先创建formater然后指定style,最后是输出string.不指定style无法输出string
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    // dateFormat.dateStyle = NSDateFormatterFullStyle;
    // dateFormat.timeStyle = NSDateFormatterFullStyle;
    dateFormat.dateStyle = NSDateFormatterMediumStyle;
    dateFormat.timeStyle = NSDateFormatterMediumStyle;
    // dateFormat.dateStyle = NSDateFormatterShortStyle;
    // dateFormat.timeStyle = NSDateFormatterShortStyle;


    //列出当前系统支持的时区格式,看城市名字就可以知道是哪个国家的时区
    // NSLog(@"tiemeZone %@",[NSTimeZone knownTimeZoneNames]);
    //获取当前系统所在时区的名称
    // NSLog(@"tiemeZone: %@", [NSTimeZone name]);

    //列出的local都是en_US这样的,不知道具体代表哪个国家
     // NSLog(@"locale: %@",[NSLocale availableLocaleIdentifiers]);

    //设置地区为控件日期和时间和格式,设置时区来控制时间
    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    dateFormat.timeZone = [[NSTimeZone alloc] initWithName:@"America/New_York"];
    NSLog(@"us date: %@",[dateFormat stringFromDate:date1]);

    // UTC或者UTC+/-时差, 这样很方便,省得记名字
    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
    dateFormat.timeZone = [[NSTimeZone alloc] initWithName:@"UTC+1"];
    NSLog(@"fr date: %@",[dateFormat stringFromDate:date1]);

    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_GB"];
    dateFormat.timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    NSLog(@"zh date: %@",[dateFormat stringFromDate:date1]);

    //自定义格式不是很好用,完整的格式是 yyyyMMdd hh:mm:ss,H大写是24小时格式,小写会显示am/pm,yy表示年没有20,可以只有月和日,格式为MMdd
    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_GB"];
    [dateFormat setLocalizedDateFormatFromTemplate:@"yyyyMMdd  HH:mm:ss"];
    NSLog(@"DIY formate date: %@",[dateFormat stringFromDate:date1]);


    //locale与格式都要保持一致才可以获取到日期,我举两个例子才得出的规律,en和ch两个日期格式
    //使用NSDateFormatter把特定的日期和时间转换为指定地区和日期和时间(NSString转换成NSDate)
    NSString *strDate = @"2021-11-13 16:27:00";
    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_GB"];
    [dateFormat setLocalizedDateFormatFromTemplate:@"yyyyMMdd HH:mm:ss"];
    NSLog(@"date of String: %@",[dateFormat dateFromString:strDate]);

    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [dateFormat setLocalizedDateFormatFromTemplate:@"MMddyy, HH:mm:ss"];
    strDate = @"11-13-21, 16:27:00";
    NSLog(@"date of String: %@",[dateFormat dateFromString:strDate]);


    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
    // NSCalendar * calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    // components.year = 2021;
    // components.month = 12;
    // [components setDay:5];

    [components setValue:2021 forComponent:NSCalendarUnitYear];
    [components setValue:8 forComponent:NSCalendarUnitMonth];
    [components setValue:21 forComponent:NSCalendarUnitDay];
    [components setValue:16 forComponent:NSCalendarUnitHour];
    //合并操作
    NSDate *date4 = [calendar dateFromComponents:components];
    //输出日期时对时间做了时区转换操作,合并日期和时间单元时没有时区转换操作
    NSLog(@"new date: %@, Hour:%ld",date4,components.hour);

    //分析操作,在这里分析了年月日和时分,没有分解秒
    NSCalendarUnit unitFlags = NSCalendarUnitMonth|NSCalendarUnitYear|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute;
    components = [calendar components: unitFlags fromDate:date2];
    NSLog(@"components: %@",components);
    NSLog(@"Year: %ld,Month:%ld,Day:%ld,- H:%ld, M:%ld, S:%ld",components.year,components.month,
        components.day,components.hour,components.minute,components.second);

}


在代码中我们添加了相关的注释,以方便大家理解代码。程序中的内容比较多,希望大家结合前面章回的内容一边动手是运行程序,一边分析代码,特别是地区和时区的差异。


看官们,本章回的内容就介绍到这里,欲知后事如何且听下回分解!

Guess you like

Origin blog.csdn.net/talk_8/article/details/121458910
Recommended