IOS 获取农历方法(转)
声明:以下为使用iOS的 NSChineseCalendar 网上之前发现有人说这个方法不是完全准确,有些日期会显示的不对,本人没有验证过,也实在懒得用C++那套方法去实现。
另外我做的不过是个简单的功能,还不包括什么节气 节日那些复杂有高端的功能,如果哪位大神不吝赐教 小弟在此感激了。
方法总的说来就这样:
1 +(NSString*)getChineseCalendarWithDate:(NSDate *)date{ 2 3 4 5 NSArray *chineseYears = [NSArray arrayWithObjects: 6 7 @"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉", 8 9 @"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛己", @"壬午", @"癸未", 10 11 @"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳", 12 13 @"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸丑", 14 15 @"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑", 16 17 @"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥", nil]; 18 19 20 21 NSArray *chineseMonths=[NSArray arrayWithObjects: 22 23 @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", 24 25 @"九月", @"十月", @"冬月", @"腊月", nil]; 26 27 28 29 30 31 NSArray *chineseDays=[NSArray arrayWithObjects: 32 33 @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", 34 35 @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", 36 37 @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil]; 38 43 NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar]; 44 45 46 47 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; 48 49 50 51 NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date]; 52 53 54 55 NSLog(@"%d_%d_%d %@",localeComp.year,localeComp.month,localeComp.day, localeComp.date); 56 57 58 59 NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1]; 60 61 NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1]; 62 63 NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1]; 64 65 66 67 NSString *chineseCal_str =[NSString stringWithFormat: @"%@_%@_%@",y_str,m_str,d_str]; 68 69 70 71 [localeCalendar release]; 72 73 74 75 return chineseCal_str; 76 77 }
如果需要也可以拆成单独的函数来方便获取,例如获取月份:
1 +(NSString*)getChineseMonthWithDate:(NSDate *)date{ 2 3 4 5 NSArray *chineseMonths=[NSArray arrayWithObjects: 6 7 @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", 8 9 @"九月", @"十月", @"冬月", @"腊月", nil]; 10 11 12 13 14 15 NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar]; 16 17 18 19 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; 20 21 22 23 NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date]; 24 25 26 27 //NSLog(@"%d_%d_%d %@",localeComp.year,localeComp.month,localeComp.day, localeComp.date); 28 29 30 31 NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1]; 32 33 34 35 [localeCalendar release]; 36 37 38 39 return m_str; 40 41 42 43 }
另外如果不知道如何获取NSDate 那么额外加个方法用来根据string和format来得到对应的 NSDate:
1 +(NSDate *)getDateWithDateString:(NSString *)strDate formatString:(NSString*)strFormat{ 2 3 4 5 NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 6 7 8 9 NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 10 11 12 13 //NSTimeZone *localTime = [NSTimeZone localTimeZone]; 14 15 16 17 [formatter setTimeZone:timeZone]; 18 19 [formatter setDateFormat : strFormat]; 20 21 22 23 NSDate *dateTime = [formatter dateFromString:strDate]; 24 25 26 27 return dateTime; 28 29 }
使用方法类似这样,比如获取世界末日那一时刻的Date~~~
1 NSDate *date = [Tool_Functions getDateWithDateString:@"2012-12-21 15:14:35" 2 3 formatString:@"yyyy-MM-dd HH:mm:ss"];
转载文章,原文点击