Cocoa公历和中国农历直接的转换

版权声明:大熊猫猪·侯佩原创或翻译作品.谢绝转载! hopy https://blog.csdn.net/mydo/article/details/46770145

看过某书上面的做法是先生成一个公历的calendar,使用的是:

NSCalendar *cal = [NSCalendar currentCalendar];

然后用它生成一个NSDateComponent,最后再用一个中国农历的calendar将NSDateComponent转换为一个农历的NSDate。
实际执行后发现转换后的日期是错的。
下面是一个正确的转换方法,关键在于:先生成一个农历的calendar,然后再转换为公历的表示:

void show_local_date(NSDate *date){
    NSDateFormatter *f = [NSDateFormatter new];
    [f setDateFormat:@"Y-M-d HH:mm:ss z"];
    [f setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
    NSLog(@"%s:date is %@",__func__,[f stringFromDate:date]);
}

int main(void){
    @autoreleasepool{
        NSLog(@"now is %@",[NSDate new]);
        NSCalendar *chinese_cal = [[NSCalendar alloc] initWithCalendarIdentifier:
            NSCalendarIdentifierChinese];
        [chinese_cal setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
        unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay |\
            NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        NSDateComponents *comps = [chinese_cal components:unitFlags fromDate:[NSDate new]];

        NSCalendar *cal = [NSCalendar currentCalendar];
        NSDate *china_now = [cal dateFromComponents:comps];
        show_local_date(china_now);
        NSLog(@"%@",[Foo getChineseCalendarWithDate:[NSDate new]]);
    }
    return 0;
}

最后是其他人写的转换代码,和我的类似:

+(NSString*)getChineseCalendarWithDate:(NSDate *)date{

    NSArray *chineseYears = [NSArray arrayWithObjects:
                       @"甲子", @"乙丑", @"丙寅", @"丁卯",  @"戊辰",  @"己巳",  @"庚午",  @"辛未",  @"壬申",  @"癸酉",
                       @"甲戌",   @"乙亥",  @"丙子",  @"丁丑", @"戊寅",   @"己卯",  @"庚辰",  @"辛己",  @"壬午",  @"癸未",
                       @"甲申",   @"乙酉",  @"丙戌",  @"丁亥",  @"戊子",  @"己丑",  @"庚寅",  @"辛卯",  @"壬辰",  @"癸巳",
                       @"甲午",   @"乙未",  @"丙申",  @"丁酉",  @"戊戌",  @"己亥",  @"庚子",  @"辛丑",  @"壬寅",  @"癸丑",
                       @"甲辰",   @"乙巳",  @"丙午",  @"丁未",  @"戊申",  @"己酉",  @"庚戌",  @"辛亥",  @"壬子",  @"癸丑",
                       @"甲寅",   @"乙卯",  @"丙辰",  @"丁巳",  @"戊午",  @"己未",  @"庚申",  @"辛酉",  @"壬戌",  @"癸亥", nil];

    NSArray *chineseMonths=[NSArray arrayWithObjects:
                        @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", 
                        @"九月", @"十月", @"冬月", @"腊月", nil];


    NSArray *chineseDays=[NSArray arrayWithObjects:
                      @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", 
                      @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
                      @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十",  nil];


    NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];

    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay |\
        NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

    NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];

    NSLog(@"%ld_%ld_%ld  %@",localeComp.year,localeComp.month,localeComp.day, localeComp.date);

    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];

    //[localeCalendar release];

    return chineseCal_str;
}
阅读更多

扫码向博主提问

大熊猫侯佩

博客专家

iOS以及ROR、Swift、ObjC
  • 擅长领域:
  • iOS开发
  • ROR开发
  • Swift
  • C及汇编语言
  • 框架设计
去开通我的Chat快问
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页