求两个时间差

求两个时间差,得到另一个新的时间 
Java代码   收藏代码
  1. NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象  
  2.   
  3. NSDateComponents *shibo = [[NSDateComponents alloc] init];//初始化目标时间  
  4.     [shibo setYear:2011];  
  5.     [shibo setMonth:5];  
  6.     [shibo setDay:1];  
  7.     [shibo setHour:8];  
  8.     [shibo setMinute:0];  
  9.     [shibo setSecond:0];  
  10. NSDate *todate = [cal dateFromComponents:shibo];//把目标时间装载入date  
  11.   
  12. NSDate *today = [NSDate date];//得到当前时间  
  13.   
  14. //用来得到具体的时差,位运算  
  15. unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ;  
  16.           
  17. NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0 ];  
  18. NSLog(@"%@", [NSString stringWithFormat:@"%d年%d月%d日%d时%d分%d秒",[d year],[d month], [d day], [d hour], [d minute], [d second]]);  


DEMO 2 
Java代码   收藏代码
  1. //得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:  
  2.     NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];  
  3.     NSLog(@"yesterday:%@",yesterday);  
  4.       
  5.       
  6.     NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];  
  7.     NSDate *date = [NSDate date];  
  8.     [formatter setTimeStyle:NSDateFormatterMediumStyle];  
  9.     NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];  
  10.     //NSCalendar *calendar = [NSCalendar currentCalendar];  
  11.     NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];  
  12.     NSInteger unitFlags = NSYearCalendarUnit |   
  13.     NSMonthCalendarUnit |  
  14.     NSDayCalendarUnit |   
  15.     NSWeekdayCalendarUnit |   
  16.     NSHourCalendarUnit |  
  17.     NSMinuteCalendarUnit |  
  18.     NSSecondCalendarUnit;  
  19.     //int week=0;  
  20.     comps = [calendar components:unitFlags fromDate:date];  
  21.     int week = [comps weekday];   
  22.     int year=[comps year];   
  23.     int month = [comps month];  
  24.     int day = [comps day];  
  25.     //[formatter setDateStyle:NSDateFormatterMediumStyle];  
  26.     //This sets the label with the updated time.  
  27.     int hour = [comps hour];  
  28.     int min = [comps minute];  
  29.     int sec = [comps second];  
  30.     NSLog(@"week%d",week);  
  31.     NSLog(@"year%d",year);  
  32.     NSLog(@"month%d",month);  
  33.     NSLog(@"day%d",day);  
  34.     NSLog(@"hour%d",hour);  
  35.     NSLog(@"min%d",min);  
  36.     NSLog(@"sec%d",sec);  
  37.       
  38.       
  39.       
  40.     //得到毫秒  
  41.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
  42.     [dateFormatter setDateStyle:NSDateFormatterMediumStyle];  
  43.     [dateFormatter setTimeStyle:NSDateFormatterShortStyle];  
  44.     //[dateFormatter setDateFormat:@"hh:mm:ss"];  
  45.     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];  
  46.     NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);  
  47.     [dateFormatter release];  
  48.   
  49. //[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];  
  50.      //dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];  
  51.     //[dateFormatter setTimeStyle:kCFDateFormatterMediumStyle];    
  52.     //dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];  
  53.     //dateFormatter.timeZone = [NSTimeZone defaultTimeZone];  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值