iOS

NSLocale-NSDateFormatter-NSTimeZ

2016-11-01  本文已影响97人  码农二哥

NSTimeZone

// 这个名字很委婉,known一词说明这是“他”已知的时区的名字。世界各地对自己所在的时区可能都有一定的命名,但是不一定被“他”收录。例如,中国大陆,只有重庆和上海被收录了(难道这是中国只使用一个时区的错误?!)。使用这个方法获得的时区名字,都是在iOS系统中/usr/share/zoneinfo/目录中保存时区数据。随着iOS版本的更新,这里面的数据会发生变动。当然,要是你的设备越狱了,你可以手动往该目录下添加时区文件。        
// 时区文件里面包括了一下内容:
//        当前时区相对于GMT的偏移量(s)
//        当前时区的名字缩写
//        当前时区是否使“夏时制”时区
// 因为时区文件中包含了"偏移量",所以通过“时区的名称”可以指定一个“时区”。
// 时区名称举例:
//        Africa/Abidjan
//        America/New_York
//        Asia/Shanghai
//        Asia/Hong_Kong
// 越狱的童鞋可以看出时区的名称和/usr/share/zoneinfo中的目录结构基本一一对应。
+ (NSArray *)knownTimeZoneNames;
// 获取所有的时区名称缩写
// 名称缩写与名称是一一对应的关系,例如:HKT = "Asia/Hong_Kong";
// 默认情况下,调用该方法回去/usr/share/zoneinfo目录下找时区名称缩写,但是当使用方法"+ (void)setAbbreviationDictionary:(NSDictionary *)dict;"后,将会只返回刚才设置的时区名称缩写。请看下文的代码实例!
// 名称缩写举例:
//        EST = "America/New_York";
//        GMT = GMT;
//        GST = "Asia/Dubai";
//        HKT = "Asia/Hong_Kong";
+ (NSDictionary *)abbreviationDictionary;

//  由时区的名称获得对应的NSTimeZone对象
//  通过时区名称可以获得时区文件,通过时区文件就可以获得“偏移量”,“名称缩写”,“是否使用夏时制”等信息。
+ (id)timeZoneWithName:(NSString *)tzName;

//    由时区名称缩写获得对应的NSTimeZone对象
//    这里的时区名称缩写有两种情况:
//    第一种是上面说的HKT这样的缩写,与时区名称一一对应,通过这样的缩写获得的NSTimeZone对象,与使用时区名称获得得NSTimeZone对象一样。(大概读取得是同一个时区文件)
//    第二种是"GMT+0800"这样格式得缩写,其实这就是偏移量。通过偏移量在iOS中是不能读到与之对应得时区文件的,因此就无法知道“时区名称”,“名称缩写”,“是否使用夏时制”这样的信息了。默认情况下,"时区名称"和"名称缩写"都会赋值为"GMT+0800","是否使用夏时制"则不会设置(默认不使用)。
+ (id)timeZoneWithAbbreviation:(NSString *)abbreviation;

//    由偏移量获得对应的NSTimeZone对象
//    只说一点:通过偏移量获得的NSTimeZone对象的“市区名称”,“名称缩写”都会赋值为"GMT+0800","是否使用夏时制"则不会设置(默认不使用)。
//  注意!!!!该方法不做参数的范围检查!!!
+ (id)timeZoneForSecondsFromGMT:(NSInteger)seconds;       // 不做安全性检查

NSDate

{
        NSDate *dtNow = [NSDate date];
        NSDate *dtFromNow = [NSDate dateWithTimeIntervalSinceNow:60*60];
        NSDate *dtFrom1970 = [NSDate dateWithTimeIntervalSince1970:60*60];
        NSDate *dtFrom = [[NSDate alloc] initWithTimeInterval:60*60 sinceDate:dtFromNow];
        NSDate *distantPast = [NSDate distantPast];
        NSDate *distantFuture = [NSDate distantFuture];
    }

NSCalendar

{
        //先定义一个遵循某个历法的日历对象
        NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//公历(常用),应该就是常说的阳历
        //通过已定义的日历对象,获取某个时间点的NSDateComponents表示,并设置需要表示哪些信息(NSYearCalendarUnit, NSMonthCalendarUnit, NSDayCalendarUnit等)
        NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:[NSDate date]];
        NSLog(@"year(年份): %i", dateComponents.year);
        NSLog(@"quarter(季度):%i", dateComponents.quarter);
        NSLog(@"month(月份):%i", dateComponents.month);
        NSLog(@"day(日期):%i", dateComponents.day);
        NSLog(@"hour(小时):%i", dateComponents.hour);
        NSLog(@"minute(分钟):%i", dateComponents.minute);
        NSLog(@"second(秒):%i", dateComponents.second);
        NSLog(@"weekday(星期):%i", dateComponents.weekday);
    }

NSDateFormatter, NSLocale怎么用

{
    NSString *strLocaleIdentifier = [[NSLocale currentLocale] localeIdentifier];
    NSLog(@"%@", strLocaleIdentifier);//我的模拟器的在当前设置下的输出结果:zh-Hans_AM
    
    //获取系统当前时间
    NSDate *date = [NSDate date];
    
    //格式转换
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"cccc";//星期
    NSLog(@"%@", [df stringFromDate:date]);//系统语言设置不同(设置-通用-语言与地区),输出结果可能不同
    
    df.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    NSLog(@"%@", [df stringFromDate:date]);//星期二
    
    df.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    NSLog(@"%@", [df stringFromDate:date]);//Tuesday
}

References

  1. http://kevin-wu.net/tag/nsgregoriancalendar/
  2. https://my.oschina.net/yongbin45/blog/151376
  3. http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
上一篇 下一篇

猜你喜欢

热点阅读