java 获取两个日期的天数间隔

本文介绍了一个实用的Java工具类,用于计算两个日期之间的天数间隔。利用Java 8的LocalDate和ChronoUnit,该工具类能够高效准确地计算出日期差。

java 获取两个日期的天数间隔

主要使用 java8 的

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

package com.example.books.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateTimeUtil {

    /**
     * 时间戳转日期时间
     * @param s
     * @return
     */
    public static Date stampToDate(String s){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        return new Date(lt);
    }

    /**
     * 计算现在到某一个日期的间隔天数
     * @param endDate
     * @return
     */
    public static long currentToEnd(Date endDate){
        String[] currentStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).split("-");
        String[] endStr = new SimpleDateFormat("yyyy-MM-dd").format(endDate).split("-");
        Integer currentYear = Integer.parseInt(currentStr[0]);
        Integer currentMonth = Integer.parseInt(currentStr[1]);
        Integer currentDay = Integer.parseInt(currentStr[2]);
        Integer endYear = Integer.parseInt(endStr[0]);
        Integer endMonth = Integer.parseInt(endStr[1]);
        Integer endDay = Integer.parseInt(endStr[2]);
        LocalDate endLocalDate = LocalDate.of(endYear,endMonth,endDay);
        return LocalDate.now().until(endLocalDate,ChronoUnit.DAYS);
    }

    /**
     * 计算两个日期之间的间隔天数
     * @param startDate
     * @param endDate
     * @return
     */
    public static long startToEnd(Date startDate, Date endDate){
        String[] startStr = new SimpleDateFormat("yyyy-MM-dd").format(startDate).split("-");
        String[] endStr = new SimpleDateFormat("yyyy-MM-dd").format(endDate).split("-");
        Integer startYear = Integer.parseInt(startStr[0]);
        Integer startMonth = Integer.parseInt(startStr[1]);
        Integer startDay = Integer.parseInt(startStr[2]);
        Integer endYear = Integer.parseInt(endStr[0]);
        Integer endMonth = Integer.parseInt(endStr[1]);
        Integer endDay = Integer.parseInt(endStr[2]);
        LocalDate endLocalDate = LocalDate.of(endYear,endMonth,endDay);
        LocalDate startLocalDate = LocalDate.of(startYear,startMonth,startDay);
        return startLocalDate.until(endLocalDate,ChronoUnit.DAYS);
    }

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        try {
            startDate = sdf.parse("2018-09-01 15:42:55");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("currentToEnd:"+DateTimeUtil.currentToEnd(startDate));
    }

}
Java 8 中,可以使用 `java.time` 包下的类来获取两个日期间隔天数。以下是几种常见的方法: ### 使用 `ChronoUnit.DAYS.between` 方法 `ChronoUnit` 是一个枚举类,提供了各种时间单位的计算功能。`DAYS.between` 方法可以直接计算两个 `LocalDate` 之间的天数间隔。 ```java import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class CalculateDaysInterval { 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); } } ``` 在上述代码中,首先使用 `LocalDate.of` 方法创建了起始日期和结束日期的 `LocalDate` 对象,然后使用 `ChronoUnit.DAYS.between` 方法计算两个日期之间的天数间隔,并将结果存储在 `days` 变量中,最后将结果输出到控制台 [^1]。 ### 使用 `Period` 类 `Period` 类用于表示日期之间的间隔,它可以精确到年、月、日。可以通过 `Period.between` 方法计算两个日期之间的间隔,然后获取其中的天数。 ```java import java.time.LocalDate; import java.time.Period; public class CalculateDaysIntervalWithPeriod { public static void main(String[] args) { LocalDate startDate = LocalDate.of(2021, 3, 1); LocalDate endDate = LocalDate.of(2021, 7, 8); Period period = Period.between(startDate, endDate); long days = period.getDays(); System.out.println("两个时间之间的天数间隔为:" + days); } } ``` 在上述代码中,使用 `Period.between` 方法计算两个日期之间的间隔,返回一个 `Period` 对象,然后通过 `getDays` 方法获取其中的天数,并将结果输出到控制台。 ### 相关问题 1. Java 8 中计算两个日期的月数间隔该如何实现? 2. 使用 `Period` 类计算日期间隔和 `ChronoUnit` 有什么区别? 3. 在 Java 8 中如何处理包含时间的日期对象之间的天数间隔计算? 4. 若日期对象包含时区信息,Java 8 怎样计算日期间隔天数? 5. Java 8 中计算日期间隔天数时,遇到闰年该如何处理?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值