iOS 时间转换总结

 
   //获取当前时间 
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease];
    [formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
    [formatter setDateFormat:@"yy-MM-dd HH:mm"];
    NSString *currentTime = [formatter stringFromDate:[NSDate date]];

  //时间戳转换成“2012-11-20”格式
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"wuhan"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:[time integerValue]];
    NSString *day = [dateFormatter stringFromDate:theday];
    [dateFormatter release];

  //“2012-11-20”转化成“2012年11月20日”
   NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
   [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [formatter dateFromString:time];
   [formatter setDateFormat:@"yyyy年MM月dd日"];
   NSString *day = [formatter stringFromDate:date];
   [formatter release];



  //时间差值计算
   NSString *hdate = [NSString stringWithFormat:@"%@ 12:00:00",messageDetail.action.time];
   NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
   [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
   [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   NSDate *date = [formatter dateFromString:hdate];
   [formatter release];
   NSLog(@"date = %@",date);
   NSTimeInterval late = [date timeIntervalSince1970]*1;
   NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
   NSTimeInterval nowt = [dat timeIntervalSince1970]*1;  
   NSTimeInterval cha = late - nowt;
               
   if ( cha > 0) {
        condition.date = messageDetail.action.time;
     }else{
                   
          NSDate *nowTime = [NSDate date];
           NSDate *tomorrow = [NSDate dateWithTimeInterval:24*60*60 sinceDate:nowTime];
          NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
          [formatter setDateFormat:@"yyyy-MM-dd"];
           NSString *nextDay = [formatter stringFromDate:tomorrow];
           NSLog(@"我想要看到的结果*****#####%@",nextDay);
           condition.date = nextDay;
          [formatter release];
        }
               

   //计算时间差
- (NSString *)theTime:(NSString *)issueTime
{
    //计算时间差
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:issueTime];
    [formatter release];
   
   
    NSTimeInterval late = [date timeIntervalSince1970]*1;
   
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now = [dat timeIntervalSince1970]*1;
    NSString *timeString = @"";
   
    NSTimeInterval cha = now - late;
   
    if (cha/60 > 0 && cha/60 < 1) {
        timeString = [NSString stringWithFormat:@"%f", cha];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@秒前", timeString];
       
    }
    if (cha/60 > 1 && cha/3600 < 1) {
        timeString = [NSString stringWithFormat:@"%f", cha/60];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
       
    }
    if (cha/3600 > 1 && cha/86400 < 1) {
        timeString = [NSString stringWithFormat:@"%f", cha/3600];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@小时前", timeString];
    }
    if (cha/86400 > 1)
    {
        timeString = [NSString stringWithFormat:@"%f", cha/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@天前", timeString];
    }
    return timeString;
}


//计算星期几
- (NSString *)getWeekDayByDate:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *weekComp = [calendar components:NSWeekdayCalendarUnit fromDate:date];
    NSInteger weekDayEnum = [weekComp weekday];
    NSString *weekDays = nil;
    switch (weekDayEnum) {
        case 1:
            weekDays = @"星期日";
            break;
        case 2:
            weekDays = @"星期一";
            break;
        case 3:
            weekDays = @"星期二";
            break;
        case 4:
            weekDays = @"星期三";
            break;
        case 5:
            weekDays = @"星期四";
            break;
        case 6:
            weekDays = @"星期五";
            break;
        case 7:
            weekDays = @"星期六";
            break;
        default:
            break;
    }
    return weekDays;
}


//获取农历节日,节气

获取农历节日的算法:

  • +(NSString *)getLunarHoliDayDate:(NSDate *)date{
  • NSTimeInterval timeInterval_day = float(60*60*24);
  • NSDate *nextDay_date = [NSDate dateWithTimeInterval:timeInterval_day sinceDate:date];
  • NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
  • unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
  • NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:nextDay_date];
  • if ( 1 == localeComp.month && 1 == localeComp.day ) {
  • [localeCalendar release];
  • return @"除夕";
  • }
  • NSDictionary *chineseHoliDay = [NSDictionary dictionaryWithObjectsAndKeys:
  • @"春节", @"1-1",
  • @"元宵", @"1-15",
  • @"端午", @"5-5",
  • @"七夕", @"7-7",
  • @"中元", @"7-15",
  • @"中秋", @"8-15",
  • @"重阳", @"9-9",
  • @"腊八", @"12-8",
  • @"小年", @"12-24",
  • nil];
  • localeComp = [localeCalendar components:unitFlags fromDate:date];
  • NSString *key_str = [NSString stringWithFormat:@"%d-%d",localeComp.month,localeComp.day];
  • return [chineseHoliDay objectForKey:key_str];
  • }

 

 

 

以下是根据公里年月日来获取农历节气的方法:

 

 

记录表:

  • const int START_YEAR =1901;
  • const int END_YEAR =2050;
  • static int32_t gLunarHolDay[]=
  • {
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1901
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X87, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1902
  • 0X96, 0XA5, 0X87, 0X96, 0X87, 0X87, 0X79, 0X69, 0X69, 0X69, 0X78, 0X78, //1903
  • 0X86, 0XA5, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X78, 0X87, //1904
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1905
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1906
  • 0X96, 0XA5, 0X87, 0X96, 0X87, 0X87, 0X79, 0X69, 0X69, 0X69, 0X78, 0X78, //1907
  • 0X86, 0XA5, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1908
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1909
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1910
  • 0X96, 0XA5, 0X87, 0X96, 0X87, 0X87, 0X79, 0X69, 0X69, 0X69, 0X78, 0X78, //1911
  • 0X86, 0XA5, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1912
  • 0X95, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1913
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1914
  • 0X96, 0XA5, 0X97, 0X96, 0X97, 0X87, 0X79, 0X79, 0X69, 0X69, 0X78, 0X78, //1915
  • 0X96, 0XA5, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1916
  • 0X95, 0XB4, 0X96, 0XA6, 0X96, 0X97, 0X78, 0X79, 0X78, 0X69, 0X78, 0X87, //1917
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X77, //1918
  • 0X96, 0XA5, 0X97, 0X96, 0X97, 0X87, 0X79, 0X79, 0X69, 0X69, 0X78, 0X78, //1919
  • 0X96, 0XA5, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1920
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X78, 0X79, 0X78, 0X69, 0X78, 0X87, //1921
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X77, //1922
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X87, 0X79, 0X79, 0X69, 0X69, 0X78, 0X78, //1923
  • 0X96, 0XA5, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1924
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X78, 0X79, 0X78, 0X69, 0X78, 0X87, //1925
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1926
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X87, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1927
  • 0X96, 0XA5, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1928
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1929
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1930
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X87, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1931
  • 0X96, 0XA5, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1932
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1933
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1934
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1935
  • 0X96, 0XA5, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1936
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1937
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1938
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1939
  • 0X96, 0XA5, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1940
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1941
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1942
  • 0X96, 0XA4, 0X96, 0X96, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1943
  • 0X96, 0XA5, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1944
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1945
  • 0X95, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X78, 0X69, 0X78, 0X77, //1946
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1947
  • 0X96, 0XA5, 0XA6, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //1948
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X79, 0X78, 0X79, 0X77, 0X87, //1949
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X78, 0X79, 0X78, 0X69, 0X78, 0X77, //1950
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X79, 0X79, 0X79, 0X69, 0X78, 0X78, //1951
  • 0X96, 0XA5, 0XA6, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //1952
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1953
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X78, 0X79, 0X78, 0X68, 0X78, 0X87, //1954
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1955
  • 0X96, 0XA5, 0XA5, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //1956
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1957
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1958
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1959
  • 0X96, 0XA4, 0XA5, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //1960
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1961
  • 0X96, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1962
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1963
  • 0X96, 0XA4, 0XA5, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //1964
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1965
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1966
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1967
  • 0X96, 0XA4, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //1968
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1969
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1970
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X79, 0X69, 0X78, 0X77, //1971
  • 0X96, 0XA4, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //1972
  • 0XA5, 0XB5, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1973
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1974
  • 0X96, 0XB4, 0X96, 0XA6, 0X97, 0X97, 0X78, 0X79, 0X78, 0X69, 0X78, 0X77, //1975
  • 0X96, 0XA4, 0XA5, 0XB5, 0XA6, 0XA6, 0X88, 0X89, 0X88, 0X78, 0X87, 0X87, //1976
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //1977
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X78, 0X87, //1978
  • 0X96, 0XB4, 0X96, 0XA6, 0X96, 0X97, 0X78, 0X79, 0X78, 0X69, 0X78, 0X77, //1979
  • 0X96, 0XA4, 0XA5, 0XB5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //1980
  • 0XA5, 0XB4, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X77, 0X87, //1981
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1982
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X78, 0X79, 0X78, 0X69, 0X78, 0X77, //1983
  • 0X96, 0XB4, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X87, //1984
  • 0XA5, 0XB4, 0XA6, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //1985
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1986
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X79, 0X78, 0X69, 0X78, 0X87, //1987
  • 0X96, 0XB4, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //1988
  • 0XA5, 0XB4, 0XA5, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //1989
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //1990
  • 0X95, 0XB4, 0X96, 0XA5, 0X86, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1991
  • 0X96, 0XB4, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //1992
  • 0XA5, 0XB3, 0XA5, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //1993
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1994
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X76, 0X78, 0X69, 0X78, 0X87, //1995
  • 0X96, 0XB4, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //1996
  • 0XA5, 0XB3, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //1997
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //1998
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //1999
  • 0X96, 0XB4, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //2000
  • 0XA5, 0XB3, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //2001
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //2002
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //2003
  • 0X96, 0XB4, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //2004
  • 0XA5, 0XB3, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //2005
  • 0XA5, 0XB4, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //2006
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X69, 0X78, 0X87, //2007
  • 0X96, 0XB4, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X87, 0X78, 0X87, 0X86, //2008
  • 0XA5, 0XB3, 0XA5, 0XB5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //2009
  • 0XA5, 0XB4, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //2010
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X78, 0X87, //2011
  • 0X96, 0XB4, 0XA5, 0XB5, 0XA5, 0XA6, 0X87, 0X88, 0X87, 0X78, 0X87, 0X86, //2012
  • 0XA5, 0XB3, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X87, //2013
  • 0XA5, 0XB4, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //2014
  • 0X95, 0XB4, 0X96, 0XA5, 0X96, 0X97, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //2015
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA6, 0X87, 0X88, 0X87, 0X78, 0X87, 0X86, //2016
  • 0XA5, 0XC3, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X87, //2017
  • 0XA5, 0XB4, 0XA6, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //2018
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //2019
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA6, 0X97, 0X87, 0X87, 0X78, 0X87, 0X86, //2020
  • 0XA5, 0XC3, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //2021
  • 0XA5, 0XB4, 0XA5, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //2022
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X79, 0X77, 0X87, //2023
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA6, 0X97, 0X87, 0X87, 0X78, 0X87, 0X96, //2024
  • 0XA5, 0XC3, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //2025
  • 0XA5, 0XB3, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //2026
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //2027
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA6, 0X97, 0X87, 0X87, 0X78, 0X87, 0X96, //2028
  • 0XA5, 0XC3, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //2029
  • 0XA5, 0XB3, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //2030
  • 0XA5, 0XB4, 0X96, 0XA5, 0X96, 0X96, 0X88, 0X78, 0X78, 0X78, 0X87, 0X87, //2031
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA6, 0X97, 0X87, 0X87, 0X78, 0X87, 0X96, //2032
  • 0XA5, 0XC3, 0XA5, 0XB5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X86, //2033
  • 0XA5, 0XB3, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X78, 0X88, 0X78, 0X87, 0X87, //2034
  • 0XA5, 0XB4, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //2035
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA6, 0X97, 0X87, 0X87, 0X78, 0X87, 0X96, //2036
  • 0XA5, 0XC3, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X86, //2037
  • 0XA5, 0XB3, 0XA5, 0XA5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //2038
  • 0XA5, 0XB4, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //2039
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA6, 0X97, 0X87, 0X87, 0X78, 0X87, 0X96, //2040
  • 0XA5, 0XC3, 0XA5, 0XB5, 0XA5, 0XA6, 0X87, 0X88, 0X87, 0X78, 0X87, 0X86, //2041
  • 0XA5, 0XB3, 0XA5, 0XB5, 0XA6, 0XA6, 0X88, 0X88, 0X88, 0X78, 0X87, 0X87, //2042
  • 0XA5, 0XB4, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //2043
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA6, 0X97, 0X87, 0X87, 0X88, 0X87, 0X96, //2044
  • 0XA5, 0XC3, 0XA5, 0XB4, 0XA5, 0XA6, 0X87, 0X88, 0X87, 0X78, 0X87, 0X86, //2045
  • 0XA5, 0XB3, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X88, 0X78, 0X87, 0X87, //2046
  • 0XA5, 0XB4, 0X96, 0XA5, 0XA6, 0X96, 0X88, 0X88, 0X78, 0X78, 0X87, 0X87, //2047
  • 0X95, 0XB4, 0XA5, 0XB4, 0XA5, 0XA5, 0X97, 0X87, 0X87, 0X88, 0X86, 0X96, //2048
  • 0XA4, 0XC3, 0XA5, 0XA5, 0XA5, 0XA6, 0X97, 0X87, 0X87, 0X78, 0X87, 0X86, //2049
  • 0XA5, 0XC3, 0XA5, 0XB5, 0XA6, 0XA6, 0X87, 0X88, 0X78, 0X78, 0X87, 0X87 //2050
  • };

 

 

 

 

函数:

 

  • +(NSString *)getLunarSpecialDate:(int)iYear Month:(int)iMonth Day:(int)iDay {
  • NSArray *chineseDays=[NSArray arrayWithObjects:
  • @"小寒", @"大寒", @"立春", @"雨水", @"惊蛰", @"春分",
  • @"清明", @"谷雨", @"立夏", @"小满", @"芒种", @"夏至",
  • @"小暑", @"大暑", @"立秋", @"处暑", @"白露", @"秋分",
  • @"寒露", @"霜降", @"立冬", @"小雪", @"大雪", @"冬至", nil];
  • int array_index = (iYear - START_YEAR)*12+iMonth -1 ;
  • int64_t flag = gLunarHolDay[array_index];
  • int64_t day;
  • if(iDay <15)
  • day= 15 - ((flag>>4)&0x0f);
  • else
  • day = ((flag)&0x0f)+15;
  • int index = -1;
  • if(iDay == day){
  • index = (iMonth-1) *2 + (iDay>15? 1: 0);
  • }
  • if ( index >= 0 && index < [chineseDays count] ) {
  • return [chineseDays objectAtIndex:index];
  • } else {
  • return nil;
  • }
  • }

//获取农历方法1
  1. +(NSString*)getChineseCalendarWithDate:(NSDate *)date{  
  2.   
  3.     NSArray *chineseYears = [NSArray arrayWithObjects:  
  4.                        @"甲子", @"乙丑", @"丙寅", @"丁卯",  @"戊辰",  @"己巳",  @"庚午",  @"辛未",  @"壬申",  @"癸酉",  
  5.                        @"甲戌",   @"乙亥",  @"丙子",  @"丁丑", @"戊寅",   @"己卯",  @"庚辰",  @"辛己",  @"壬午",  @"癸未",  
  6.                        @"甲申",   @"乙酉",  @"丙戌",  @"丁亥",  @"戊子",  @"己丑",  @"庚寅",  @"辛卯",  @"壬辰",  @"癸巳",  
  7.                        @"甲午",   @"乙未",  @"丙申",  @"丁酉",  @"戊戌",  @"己亥",  @"庚子",  @"辛丑",  @"壬寅",  @"癸丑",  
  8.                        @"甲辰",   @"乙巳",  @"丙午",  @"丁未",  @"戊申",  @"己酉",  @"庚戌",  @"辛亥",  @"壬子",  @"癸丑",  
  9.                        @"甲寅",   @"乙卯",  @"丙辰",  @"丁巳",  @"戊午",  @"己未",  @"庚申",  @"辛酉",  @"壬戌",  @"癸亥", nil];  
  10.       
  11.     NSArray *chineseMonths=[NSArray arrayWithObjects:  
  12.                         @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",   
  13.                         @"九月", @"十月", @"冬月", @"腊月", nil];  
  14.       
  15.       
  16.     NSArray *chineseDays=[NSArray arrayWithObjects:  
  17.                       @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",   
  18.                       @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",  
  19.                       @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十",  nil];  
  20.       
  21.       
  22.     NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];  
  23.       
  24.     unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;  
  25.   
  26.     NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];  
  27.       
  28.     NSLog(@"%d_%d_%d  %@",localeComp.year,localeComp.month,localeComp.day, localeComp.date);  
  29.       
  30.     NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1];  
  31.     NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];  
  32.     NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];  
  33.   
  34.     NSString *chineseCal_str =[NSString stringWithFormat: @"%@_%@_%@",y_str,m_str,d_str];  
  35.       
  36.     [localeCalendar release];  
  37.       
  38.     return chineseCal_str;  
  39. }  


 

如果需要也可以拆成单独的函数来方便获取,例如获取月份:

  1. +(NSString*)getChineseMonthWithDate:(NSDate *)date{  
  2.       
  3.     NSArray *chineseMonths=[NSArray arrayWithObjects:  
  4.                             @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",   
  5.                             @"九月", @"十月", @"冬月", @"腊月", nil];  
  6.       
  7.       
  8.     NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];  
  9.       
  10.     unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;  
  11.       
  12.     NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];  
  13.       
  14.     //NSLog(@"%d_%d_%d  %@",localeComp.year,localeComp.month,localeComp.day, localeComp.date);  
  15.   
  16.     NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];  
  17.       
  18.     [localeCalendar release];  
  19.       
  20.     return m_str;  
  21.   
  22. }  


 

另外如果不知道如何获取NSDate 那么额外加个方法用来根据string和format来得到对应的 NSDate:

 

 
  1. +(NSDate *)getDateWithDateString:(NSString *)strDate formatString:(NSString*)strFormat{  
  2.       
  3.     NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];   
  4.       
  5.     NSTimeZone *timeZone =  [NSTimeZone timeZoneWithAbbreviation:@"GMT"];   
  6.       
  7.     //NSTimeZone *localTime = [NSTimeZone localTimeZone];    
  8.       
  9.     [formatter setTimeZone:timeZone];    
  10.     [formatter setDateFormat : strFormat];        
  11.       
  12.     NSDate *dateTime = [formatter dateFromString:strDate];  
  13.       
  14.     return dateTime;  
  15. }  

使用方法类似这样,比如获取世界末日那一时刻的Date~~~
  1. NSDate *date = [Tool_Functions getDateWithDateString:@"2012-12-21 15:14:35"   
  2.                                             formatString:@"yyyy-MM-dd HH:mm:ss"]; 


//获取农历方法2
- (NSString *)lunarForSolar:(NSDate *)solarDate{
   
    //农历日期名
   
    NSArray *cDayName = [NSArray  arrayWithObjects:@"*",@"初一",@"初二",@"初三",@"初四",@"初五",@"初六",@"初七",@"初八",@"初九",@"初十",
                        
                         @"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十",
                        
                         @"廿一",@"廿二",@"廿三",@"廿四",@"廿五",@"廿六",@"廿七",@"廿八",@"廿九",@"三十",nil];
   
    //农历月份名
   
    NSArray *cMonName = [NSArray arrayWithObjects:@"*",@"正",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"腊",nil];
   
   
    //公历每月前面的天数
   
    const int wMonthAdd[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
   
   
   
    //农历数据
   
    const int wNongliData[100] = {2635,333387,1701,1748,267701,694,2391,133423,1175,396438
       
        ,3402,3749,331177,1453,694,201326,2350,465197,3221,3402
       
        ,400202,2901,1386,267611,605,2349,137515,2709,464533,1738
       
        ,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
       
        ,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
       
        ,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
       
        ,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
       
        ,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
       
        ,268949,3402,3493,133973,1386,464219,605,2349,334123,2709
       
        ,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877};
   
   
   
    static int wCurYear,wCurMonth,wCurDay;
   
    static int nTheDate,nIsEnd,m,k,n,i,nBit;
   
   
   
    //取当前公历年、月、日
   
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:solarDate];
   
    wCurYear = [components year];
   
    wCurMonth = [components month];
   
    wCurDay = [components day];
   
   
   
    //计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)
   
    nTheDate = (wCurYear - 1921) * 365 + (wCurYear - 1921) / 4 + wCurDay + wMonthAdd[wCurMonth - 1] - 38;
   
    if((!(wCurYear % 4)) && (wCurMonth > 2))
       
        nTheDate = nTheDate + 1;
   
   
   
    //计算农历天干、地支、月、日
   
    nIsEnd = 0;
   
    m = 0;
   
    while(nIsEnd != 1)
       
    {
       
        if(wNongliData[m] < 4095)
           
            k = 11;
       
        else
           
            k = 12;
       
        n = k;
       
        while(n>=0)
           
        {
           
            //获取wNongliData(m)的第n个二进制位的值
           
            nBit = wNongliData[m];
           
            for(i=1;i
               
                nBit = nBit/2;
           
           
           
            nBit = nBit % 2;
           
           
           
            if (nTheDate <= (29 + nBit))
               
            {
               
                nIsEnd = 1;
               
                break;
               
            }
           
           
           
            nTheDate = nTheDate - 29 - nBit;
           
            n = n - 1;
           
        }
       
        if(nIsEnd)
           
            break;
       
        m = m + 1;
       
    }
   
    wCurYear = 1921 + m;
   
    wCurMonth = k - n + 1;
   
    wCurDay = nTheDate;
   
    if (k == 12)
       
    {
       
        if (wCurMonth == wNongliData[m] / 65536 + 1)
           
            wCurMonth = 1 - wCurMonth;
       
        else if (wCurMonth > wNongliData[m] / 65536 + 1)
           
            wCurMonth = wCurMonth - 1;
       
    }
   
    //生成农历月、日
   
    NSString *szNongliDay;
   
    if (wCurMonth < 1){
       
        szNongliDay = [NSString stringWithFormat:@"闰%@",(NSString *)[cMonName objectAtIndex:-1 * wCurMonth]];
       
    }
   
    else{
       
        szNongliDay = (NSString *)[cMonName objectAtIndex:wCurMonth];
       
    }
   
    NSString *lunarDate = [NSString stringWithFormat:@"%@月%@",szNongliDay,(NSString *)[cDayName objectAtIndex:wCurDay]];
   
    return lunarDate;
     
}

//显示六天周次,当天排第一,显示为今天
- (void)weekDays{
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps;
   
    // 周几和星期几获得
    comps = [calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
                        fromDate:date];
    NSInteger weekday = [comps weekday]; // 星期几(注意,周日是“1”,周一是“2”。。。。)
    NSArray *weekDays = [NSArray arrayWithObjects:@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六", nil];
   
   
    for (int j = weekday-1; j
        [weekArray addObject:[weekDays objectAtIndex:j]];
    }
    if (weekday>2) {
        for (int k = 0; k
            [weekArray addObject:[weekDays objectAtIndex:k]];
        }
       
    }
   
    for (int i =0; i<6; i++) {
        OneDayWeatherView *oneDayWeather = (OneDayWeatherView *)[self viewWithTag:2012+i];
        oneDayWeather.theWeek.text = [weekArray objectAtIndex:i];
        if (i==0) {
            oneDayWeather.theWeek.text = @"今天";
        }
    }
}
 

 

posted on 2015-09-17 10:03  廖利君  阅读(306)  评论(0编辑  收藏  举报