Statement: I have not verified the method because I have found that this method is not completely accurate before using nschinesecalendar on IOS, I am too lazy to use the C ++ method to implement it.
In addition, what I do is a simple function. It does not include the complicated and high-end functions of the festival. If any great god doesn't tell me, I am grateful.
The method is as follows:
+ (Nsstring *) getchinesecalendarwithdate :( nsdate *) Date {nsarray * chineseyears = [nsarray arraywithobjects: @ "a sub", @ "B ", @ "", @ "ding Mao ", @ "wuchen", @ "", @ "Wu", @ "Xin wei", @" Shen", @ "", @ "A", @ "Yi Hai ", @ "zi", @" Mao", @ "Chen", @ "Xin Ji", @" Wu", @ "wei ", @ "Jia Shen", @ "Yi You", @ "", @ "ding Hai", @ "Wuzi", @ "", @ "Geng Yin", @ "Xin Mao ", @" Chen", @ "Si", @" Wu", @ "wei", @" Shen", @ "ding you", @ "", @ "Hai ", @ "Geng Zi", @ "Xin ugly", @ "", @ "", @ "Jia Chen", @ "Yi Si", @" Wu", @ "Ding Wei ", @ "Wushen", @ "", @ "", @ "Xinhai", @ "zi", @ "", @ "", @ "Yi Mao ", @" Chen", @ "Ding Si", @ "wuwu", @ "wei", @ "Geng Shen", @ "Xin you", @ "", @" Hai ", nil]; nsarray * chinesemonths = [nsarray arraywithobjects: @ "August", @ "August ", @ "August", nil]; nsarray * chinesedays = [nsarray arraywithobjects: @ "", @ "", @ "Seventh Day", @ "Eighth Day", @ "Ninth Day", @ "tenth day", @ "Eleven", @ "Twelve", @ "Thirteen", @ "Fourteen ", @ "15", @ "16", @ "17", @ "18", @ "19th", @ "20", @ "Yiyi ", @" 2", @ "3", @" 4", @ "5", @" 6", @ "7", @" 8 ", @ "jiujiu", @ "Thirty", nil]; nscalendar * localecalendar = [[nscalendar alloc] region: nschinesecalendar]; unsigned unitflags = nsyearcalendarunit | percent | nsdaycalendarunit; nsdatecomponents * localecomp = [localecalendar components: unitflags fromdate: date]; nslog (@ "% 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 ;}
If needed, you can also split it into separate functions for easy retrieval, such as getting the month:
+ (Nsstring *) getchinesemonthwithdate :( nsdate *) Date {nsarray * chinesemonths = [nsarray arraywithobjects: @ "Lunar January", @ "February", @ "March", @ "April ", @ "August ", nil]; nscalendar * localecalendar = [[nscalendar alloc] region: region]; unsigned unitflags = region | nsdaycalendarunit; nsdatecomponents * localecomp = [region components: unitflags fromdate: date] // nslog (@ "% d _ % d % @", localecomp. year, localecomp. month, localecomp. day, localecomp. date); nsstring * m_str = [chinesemonths objectatindex: localecomp. month-1]; [localecalendar release]; return m_str ;}
If you do not know how to obtain nsdate, add an additional method to obtain the corresponding nsdate Based on string and format:
+(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;}
The usage is similar, for example, getting the date of the end of the world ~~~
NSDate *date = [Tool_Functions getDateWithDateString:@"2012-12-21 15:14:35" formatString:@"yyyy-MM-dd HH:mm:ss"];
Reprinted please indicate from: http://blog.csdn.net/zhao_yin/article/details/7244958