1. 程式人生 > >java中計算時間差值的方法(天數,小時數,分鐘數,秒數)

java中計算時間差值的方法(天數,小時數,分鐘數,秒數)

藉助joda-time來計算兩個日期的差值,包含天數,小時,分鐘,秒數:

1、joda-time-2.9.9.jar包下載地址

2、程式碼例項:

package org.joda.time.test;

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

import org.joda.time.DateTimeUtils;
import org.joda.time.Interval;
import org.joda.time.Period;
/**
 * 計算兩個日期的時間差值 年 月 日 時分秒
 * @author gcc
 *
 * 2018年1月25日
 */
public class TimeDiff {
	public static void main(String[] args) {
		TimeDiff dutil = new TimeDiff();
		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");
			dutil.diff(date1,date2);
			
		} catch (ParseException e) {
		
			e.printStackTrace();
		}
        
		
	}

	private void diff(Date date1, Date date2) {
		Interval  interval = new Interval(date1.getTime(),date2.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());
		
	}

}
通過程式碼可以看出藉助joda來計算日期差值,方法簡單明瞭,推薦使用。

對於jdk自帶的API的實現:

package org.joda.time.test;

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

/**
 * JDK自帶API計算兩個日期的差值
 * @author gcc
 *
 * 2018年1月25日
 */
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);
	 
	    }
	 

}
比較繁瑣,需要計算各個時間差值。

作者:scgyus