IOS 擷取農曆方法

來源:互聯網
上載者:User

聲明:以下為使用IOS的 NSChineseCalendar 網上之前發現有人說這個方法不是完全準確,有些日期會顯示的不對,本人沒有驗證過,也實在懶得用C++那套方法去實現。

另外我做的不過是個簡單的功能,還不包括什麼節氣 節日那些複雜有高端的功能,如果哪位大神不吝賜教 小弟在此感激了。

方法總的說來就這樣: 

+(NSString*)getChineseCalendarWithDate:(NSDate *)date{    NSArray *chineseYears = [NSArray arrayWithObjects:                       @"甲子", @"乙丑", @"丙寅",@"丁卯",@"戊辰",@"己巳",@"庚午",@"辛未",@"壬申",@"癸酉",                       @"甲戌",@"乙亥",@"丙子",@"丁丑", @"戊寅",@"己卯",@"庚辰",@"辛己",@"壬午",@"癸未",                       @"甲申",@"乙酉",@"丙戌",@"丁亥",@"戊子",@"己丑",@"庚寅",@"辛卯",@"壬辰",@"癸巳",                       @"甲午",@"乙未",@"丙申",@"丁酉",@"戊戌",@"己亥",@"庚子",@"辛丑",@"壬寅",@"癸丑",                       @"甲辰",@"乙巳",@"丙午",@"丁未",@"戊申",@"己酉",@"庚戌",@"辛亥",@"壬子",@"癸丑",                       @"甲寅",@"乙卯",@"丙辰",@"丁巳",@"戊午",@"己未",@"庚申",@"辛酉",@"壬戌",@"癸亥", nil];        NSArray *chineseMonths=[NSArray arrayWithObjects:                        @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",                         @"九月", @"十月", @"冬月", @"臘月", nil];            NSArray *chineseDays=[NSArray arrayWithObjects:                      @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",                       @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",                      @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十",  nil];            NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];        unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;    NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];        NSLog(@"%d_%d_%d  %@",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;}

如果需要也可以拆成單獨的函數來方便擷取,例如擷取月份:

+(NSString*)getChineseMonthWithDate:(NSDate *)date{        NSArray *chineseMonths=[NSArray arrayWithObjects:                            @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",                             @"九月", @"十月", @"冬月", @"臘月", nil];            NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];        unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;        NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];        //NSLog(@"%d_%d_%d  %@",localeComp.year,localeComp.month,localeComp.day, localeComp.date);    NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];        [localeCalendar release];        return m_str;}

另外如果不知道如何擷取NSDate 那麼額外加個方法用來根據string和format來得到對應的 NSDate:

+(NSDate *)getDateWithDateString:(NSString *)strDate formatString:(NSString*)strFormat{        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];         NSTimeZone *timeZone =  [NSTimeZone timeZoneWithAbbreviation:@"GMT"];         //NSTimeZone *localTime = [NSTimeZone localTimeZone];          [formatter setTimeZone:timeZone];      [formatter setDateFormat : strFormat];              NSDate *dateTime = [formatter dateFromString:strDate];        return dateTime;}

使用方法類似這樣,比如擷取世界末日那一時刻的Date~~~

NSDate *date = [Tool_Functions getDateWithDateString:@"2012-12-21 15:14:35"                                             formatString:@"yyyy-MM-dd HH:mm:ss"];

轉載請標明來自:http://blog.csdn.net/zhao_yin/article/details/7244958

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.