Java:日期差异 [英] Java: date difference

查看:153
本文介绍了Java:日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java中工作。我发现两个日期之间的区别。

  public static void main(String [] args){
final Date futDate = new GregorianCalendar(2012,8,15,0,0,0).getTime();

final Date currentDate = new GregorianCalendar()。getTime();

long diff = Math.round((futDate.getTime() - currentDate.getTime())/ 1000);

System.out.println(diff / 86400 +days);
System.out.println((diff%86400)/ 3600 +hrs);
System.out.println(((diff%86400)%3600)/ 60 +mins);
System.out.println((((diff%86400)%3600)%60)%60 +secs);
}

输出:

  31天
8小时
37分钟
30秒

即使日期差异少于一天,输出超过31天。

解决方案

8不是当前的月份号码:)



月份从



Java的日期很痛苦,所以我建议您查看 http://joda-time.sourceforge.net/


I am working on dates in Java. I am finding out the difference between two dates.

public static void main(String[] args) {
    final Date futDate = new GregorianCalendar(2012, 8, 15, 0, 0, 0).getTime();

    final Date currentDate = new GregorianCalendar().getTime();

    long diff = Math.round((futDate.getTime() - currentDate.getTime()) / 1000);

    System.out.println(diff / 86400 + " days");
    System.out.println((diff % 86400) / 3600 + " hrs");
    System.out.println(((diff % 86400) % 3600) / 60 + " mins");
    System.out.println((((diff % 86400) % 3600) % 60) % 60 + " secs");
}

Output:

31 days
8 hrs
37 mins
30 secs

Even though the date difference is less than a day, the output is more than 31 days.

解决方案

8 isn't current month number :)

months are numbered from 0

Java's dates are painful, so I suggest take a look at http://joda-time.sourceforge.net/

这篇关于Java:日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆