Java計算兩個日期的時間間隔


不多說,直接上代碼

1、利用SimpleDateFormat類,獲取天數間隔 

代碼:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 利用SimpleDateFormat類計算兩個時間的天數間隔
 * @throws ParseException
 */
public class CalculateDaysInterval1 {
    public static void main(String[] args) throws ParseException {
        // 日期格式化
        DateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = simpleFormat.parse("2021-03-01");
        Date endDate = simpleFormat.parse("2021-07-08");
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        int days = (int) ((endTime - startTime) / (1000 * 60 * 60 * 24));
        System.out.println("兩個時間之間的天數間隔為:" + days);
    }
}

 輸出結果:

    兩個時間之間的天數間隔為:129

2、利用Java 8中ChronoUnit類,獲取天數間隔 

代碼:

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
/**
 * 利用ChronoUnit類計算兩個時間的天數間隔
 */
public class CalculateDaysInterval2 {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2021, 3, 1);
        LocalDate endDate = LocalDate.of(2021, 7, 8);
        long days = ChronoUnit.DAYS.between(startDate, endDate);
        System.out.println("兩個時間之間的天數間隔為:" + days);
    }
}

 輸出結果:

    兩個時間之間的天數間隔為:129

3.1、利用getTime(),獲取時分秒間隔 

public static void main(String[] args) throws ParseException {
    String str1 = "2021-09-09 03:30:16";
    String str2 = "2021-09-09 13:31:19";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date firstTime = df.parse(str1);
    Date currentTime = df.parse(str2);
    System.out.println(getTimeInterval(currentTime, firstTime));
}

/**
 * 獲取時間差方法,返回時分秒 HH:mm:ss
 *
 * @param currentTime
 * @param firstTime
 * @return
 */
public static String getTimeInterval(Date currentTime, Date firstTime) {
    DecimalFormat decimalFormat = new DecimalFormat("00");
    long diff = currentTime.getTime() - firstTime.getTime();//得到的差值
    long hours = diff / (1000 * 60 * 60); //獲取時
    long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60);  //獲取分鍾
    long s = (diff / 1000 - hours * 60 * 60 - minutes * 60);//獲取秒
    String countTime = "" + decimalFormat.format(hours) + ":" + decimalFormat.format(minutes) + ":" + decimalFormat.format(s);
    return countTime;
}

輸出結果:

  3.2、利用getTime(),獲取年月日時分秒間隔 

public static void main(String[] args) throws ParseException {
        String str1 = "2020-05-29 03:30:16";
        String str2 = "2021-09-09 13:31:19";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date firstTime = df.parse(str1);
        Date currentTime = df.parse(str2);
        System.out.println(getTimeInterval(currentTime, firstTime));
    }

    /**
     * 獲取時間差方法,返回年月日時分秒 某年某月某日 某時某分某秒
     *
     * @param currentTime
     * @param firstTime
     * @return
     */
    public static String getTimeInterval(Date currentTime, Date firstTime) {
        // 得到的時間差值, 微秒級別
        long diff = currentTime.getTime() - firstTime.getTime();
        Calendar currentTimes = dataToCalendar(currentTime);//當前系統時間轉Calendar類型
        Calendar firstTimes = dataToCalendar(firstTime);//查詢的數據時間轉Calendar類型
        int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);//獲取年
        int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
        int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
        if (day < 0) {
            month -= 1;
            currentTimes.add(Calendar.MONTH, -1);
            day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//獲取日
        }
        if (month < 0) {
            month = (month + 12) % 12;//獲取月
            year--;
        }
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);//獲取時
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);//獲取分鍾
        long s = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);//獲取秒
        String CountTime = "" + year + "年" + month + "月" + day + "天 " + hours + "時" + minutes + "分" + s + "秒";
        return CountTime;
    }

    // Date類型轉Calendar類型
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

 輸出結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM