LocalDateTime类获取当日00:00与当月第一天

本文深入讲解Java8中LocalDateTime类的使用,包括如何获取当日00:00时刻及当月第一天的日期,同时介绍了LocalDateTime与其他时间日期类的关系,以及其内部实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LocalDateTime类获取当日00:00与当月第一天

LocalDateTime类

Java8以前, Date、Calendar,DateFormat 等组成的「传统时间日期 API」,但是传统的处理接口设计并不是很友好,不易使用。终于,Java 8 借鉴第三方优秀开源库 Joda-time,重新设计了一套 API。这就是java.time包。

在java8中,java.time包下主要包含下面几个主要的类:

Instant:时间戳
Duration:持续时间,时间差
LocalDate:只包含日期,比如:2016-10-20
LocalTime:只包含时间,比如:23:12:10
LocalDateTime:包含日期和时间,比如:2016-10-20 23:14:21
Period:时间段
ZoneOffset:时区偏移量,比如:+8:00
ZonedDateTime:带时区的时间
Clock:时钟,比如获取目前美国纽约的时间

LocalDateTime类里包含了LocalDate与LocalTime类

	/**
     * The date part.
     */
    private final LocalDate date;
    /**
     * The time part.
     */
    private final LocalTime time;

获取当日00:00

LocalDateTime today_start = LocalDateTime.of(LocalDate.now(),LocalTime.MIN);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(today_start));

上述代码成功获取了当日的00:00,其中重点需要了解的是LocalDateTime的of方法
在IDEA里我们可以看到有七种声明,既可以利用LocalDate与LocalTime来初始化时间,也可以用自定义时间
如LocalDateTime.of(2020, 01, 01, 00, 00, 00);
在这里插入图片描述
我们选择的是利用LocalDate与LocalTime来初始化。
LocalDate内的静态方法now()的定义如下

public static LocalDate now() {
        return now(Clock.systemDefaultZone());
    }

用LocalDate.now()就可以返回默认区域中最佳可用系统时钟的时钟。
而LocalTime的成员变量MIN的定义如下

	/**
     * The minimum supported {@code LocalTime}, '00:00'.
     * This is the time of midnight at the start of the day.
     */
    public static final LocalTime MIN;
    /**
     * The maximum supported {@code LocalTime}, '23:59:59.999999999'.
     * This is the time just before midnight at the end of the day.
     */
    public static final LocalTime MAX;
    static {
        for (int i = 0; i < HOURS.length; i++) {
            HOURS[i] = new LocalTime(i, 0, 0, 0);
        }
        MIDNIGHT = HOURS[0];
        NOON = HOURS[12];
        MIN = HOURS[0];
        MAX = new LocalTime(23, 59, 59, 999_999_999);
    }

在LocalTime内,MIN与MAX被初始化为00:00与23:59:59.99999999。

获取当月第一天

		LocalDate thisMonth = LocalDate.now();
        LocalDate firstDayThisMonth = LocalDate.now().withDayOfMonth(1);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        System.out.println(df.format(firstDayThisMonth));

那么这段代码内最核心的就是.withDayOfMonth(1)这个方法了,下面我们来看看LocalDate内关于它的声明。

/**
     * Returns a copy of this {@code LocalDate} with the day-of-month altered.
     * <p>
     * If the resulting date is invalid, an exception is thrown.
     * <p>
     * This instance is immutable and unaffected by this method call.
     *
     * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
     * @return a {@code LocalDate} based on this date with the requested day, not null
     * @throws DateTimeException if the day-of-month value is invalid,
     *  or if the day-of-month is invalid for the month-year
     */
    public LocalDate withDayOfMonth(int dayOfMonth) {
        if (this.day == dayOfMonth) {
            return this;
        }
        return of(year, month, dayOfMonth);
    }

该方法的返回值是一个用dayOfMonth来替换原本day的一个副本。
于是得到当月第一天的方法如下:首先我们利用now()获取当前时钟的year与month,最后用我们传进去的dayOfMonth来替换当前的day。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值