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

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。

  • 15
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用引用中的代码可以获取当前月的第一天0时间。首先,使用`LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())`获取当前月的第一天,然后使用`LocalTime.MIN`将时间设置为0。所以可以使用以下代码获取当月第一天0时间: ``` LocalDateTime firstDayOfMonth = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN); ``` <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [LocalDateTime获取当日00:00当月第一天](https://blog.csdn.net/qq_41267618/article/details/102914243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [LocalDateTime 获取当前(年/月)第一天及最后一天 及 获取当天起始时间](https://blog.csdn.net/weixin_55628780/article/details/130527074)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [LocalDateTime获取当日00:00、结束时间23.59与当月第一天00.00,月末最后一天23.59](https://blog.csdn.net/weixin_48174603/article/details/108781167)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值