开发中常用Java8日期和时间相关API

1、获取某天的开始时间

    /**
     * 获取某天的开始时间
     *
     * @param zonedDateTime
     * @return
     */
    public static ZonedDateTime getDayStartDate(ZonedDateTime zonedDateTime) {
        return zonedDateTime.toLocalDate().atStartOfDay(zonedDateTime.getZone());
    }

2、获取某天的结束时间

    /**
     * 获取某天的结束时间
     *
     * @param zonedDateTime
     * @return
     */
	public static ZonedDateTime getDayEndDate(ZonedDateTime zonedDateTime) {
        return LocalDateTime.of(zonedDateTime.toLocalDate(), LocalTime.MAX).atZone(zonedDateTime.getZone());
    }

以2020年3月14日为例,获取的日期为2020-03-14T23:59:59.999999999+08:00[Asia/Shanghai],但是使用MyBatis操作数据库且字段类型为datetime时,该日期落表表中为2020-03-15 00:00:00,所以此时采用如下方法操作日期:

    /**
     * 获取某天的结束时间
     *
     * @param zonedDateTime
     * @return
     */
	public static ZonedDateTime getDayEndDate(ZonedDateTime zonedDateTime) {
        LocalDateTime localDateTime = zonedDateTime.toLocalDate().atTime(23, 59, 59);
        return localDateTime.atZone(zonedDateTime.getZone());
    }

3、获取年、月、日信息

        LocalDate localDate = LocalDate.of(2020, 3, 15);
        System.out.println(localDate.getYear() + "年" + localDate.getMonthValue() + "月" + localDate.getDayOfMonth() + "号");

4、判断两个日期是否相等

LocalDate重载了equals()方法,可以直接拿来判断两个日期是否相等

        LocalDate localDate01 = LocalDate.of(2020, 3, 14);
        LocalDate localDate02 = LocalDate.now();
        System.out.println(localDate01.equals(localDate02));

5、获取一天前/后的时间

        LocalDate now = LocalDate.now();
        System.out.println(now.plusDays(1));//获取一天后的日期
        System.out.println(now.plus(1, ChronoUnit.DAYS));//获取一天后的日期
        System.out.println(now.minusDays(1));//获取一天前的日期
        System.out.println(now.minus(1, ChronoUnit.DAYS));//获取一天前的日期

同理获取一小时前/后的时间、一个周前/后的时间、一个月前/后的时间、一年前/后的时间

6、判断日期是早于还是晚于另一个日期

LocalDate类有两类方法isBefore()isAfter()用于比较日期。调用isBefore()方法时,如果给定日期小于当前日期则返回true

        LocalDate localDate01 = LocalDate.of(2020, 3, 15);
        LocalDate localDate02 = LocalDate.of(2020, 3, 14);
        System.out.println(localDate01.isAfter(localDate02));//true
        System.out.println(localDate01.isBefore(localDate02));//false

7、判断是否是闰年

LocalDate类的isLeapYear()方法判断该实例是否是一个闰年

        LocalDate localDate = LocalDate.of(2020, 3, 15);
        System.out.println(localDate.isLeapYear());//true

8、计算两个日期之间差值

    /**
     * 计算两个日期相差天数
     *
     * @param start
     * @param end
     * @return
     */
    public static long daysBetween(ZonedDateTime start, ZonedDateTime end) {
        LocalDate startDate = start.toLocalDate();
        LocalDate endDate = end.toLocalDate();
        return startDate.until(endDate, ChronoUnit.DAYS);
    }

9、获取当月第一天和最后一天

    /**
     * 获取当月第一天
     *
     * @param localDate
     * @return
     */
    public static LocalDate getThisMonthFirstDay(LocalDate localDate) {
        return LocalDate.of(localDate.getYear(), localDate.getMonthValue(), 1);
    }

    /**
     * 获取当月最后一天
     *
     * @param localDate
     * @return
     */
    public static LocalDate getThisMonthLastDay(LocalDate localDate) {
        return LocalDate.of(localDate.getYear(), localDate.getMonthValue(), 1).plusMonths(1).minusDays(1);
    }

参考:https://blog.csdn.net/gw5205566/article/details/99673369

发布了192 篇原创文章 · 获赞 447 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_40378034/article/details/104862621