java 任意两个时间差,天数,小时数,分钟数,秒数

在java 编程中,不可避免用到计算时间差。前面我写过几篇文章,关于java 时间计算的,还有timezone 转换的文章,但没有这么具体到相差到天数,小时,分钟,秒数都列出来的情况,所以这里再总结下。
1. 用JDK 自带API 实现。
2.利用 joda time library 来实现.

首先看看用JDK 自带API 是如何实现的。
程序代码 程序代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeUtils {

    public static void main(String[] args) {

      DateTimeUtils obj = new DateTimeUtils();
      SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

      try {

        Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
        Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55");

        obj.printDifference(date1, date2);

      } catch (ParseException e) {
        e.printStackTrace();
      }

    }

    //1 minute = 60 seconds
    //1 hour = 60 x 60 = 3600
    //1 day = 3600 x 24 = 86400
    public void printDifference(Date startDate, Date endDate){

        //milliseconds
        long different = endDate.getTime() - startDate.getTime();

        System.out.println("startDate : " + startDate);
        System.out.println("endDate : "+ endDate);
        System.out.println("different : " + different);

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        System.out.printf(
            "%d days, %d hours, %d minutes, %d seconds%n",
            elapsedDays,
            elapsedHours, elapsedMinutes, elapsedSeconds);

    }

}


输出结果如下:

程序代码 程序代码

startDate : Thu Oct 10 11:30:10 SGT 2013
endDate : Sun Oct 13 20:35:55 SGT 2013
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds


看看这里的输出,并没有年,月的差别。手工计算是件麻烦事,所以一般采用了 joda time library 来实现,

程序代码 程序代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.Interval;
import org.joda.time.Period;

public class DateTimeUtils {

    public static void main(String[] args) {

      DateTimeUtils obj = new DateTimeUtils();
      SimpleDateFormat simpleDateFormat =
               new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

      try {

        Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
        Date date2 = simpleDateFormat.parse("13/11/2014 20:35:55");

        obj.printDifference(date1, date2);    

      } catch (ParseException e) {
        e.printStackTrace();
      }

    }

    public void printDifference(Date startDate, Date endDate){

      Interval interval =
               new Interval(startDate.getTime(), endDate.getTime());
      Period period = interval.toPeriod();

          System.out.printf(
           "%d years, %d months, %d days, %d hours, %d minutes, %d seconds%n",
           period.getYears(), period.getMonths(), period.getDays(),
           period.getHours(), period.getMinutes(), period.getSeconds());

    }
}


看看结果

程序代码 程序代码

1 years, 1 months, 3 days, 9 hours, 5 minutes, 45 seconds


还可以参考以下文章:
java 计算时间差
java 时区转换 计算

除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: java time
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.