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;
    }

 输出结果:

 

posted @ 2021-07-08 09:55  北国浪子  阅读(14142)  评论(0编辑  收藏  举报