简体   繁体   English

JavaFX将于下个月的第一天上市

[英]JavaFX get first day of next month

I'm trying to get the first day of the next month from a LocalDate object but have run into some issues. 我试图从LocalDate对象获取下个月的第一天,但​​遇到了一些问题。

I have a datepicker where a user can pick any date they want, without restriction and I need to get the next month's first day, this is what I've thought about doing: 我有一个日期选择器,用户可以选择他们想要的任何日期,没有限制,我需要获得下个月的第一天,这是我想到的:

LocalDate localDate = myDatePicker.getValue();
LocalTime startTime = LocalTime.of(0, 0);

LocalDate endDate = LocalDate.of(localDate.getYear(), localDate.getMonthValue() + 1, 0);

However I see a problem that may occur when choosing the month December, if that happens then the call 但是我看到选择12月份时可能会出现问题,如果发生这种情况则会发生

LocalDate.of(localDate.getYear(), localDate.getMonthValue() + 1, 0);

Should fail because I'm passing it a month value of 13. Now I could choose to check if the month value is December and if so I could add 1 to the year and start at 0 like so: 应该失败,因为我将它传递给它一个月值13.现在我可以选择检查月份值是否为12月,如果是,我可以在年份中添加1并从0开始,如下所示:

if(localDate.getMonthValue() >= 12)
    LocalDate.of(localDate.getYear() + 1, 0, 0);

However I feel like there must be a way to get around this within the class itself. 但是我觉得必须有一种方法来解决这个问题。 Does anyone know if my presumptions about passing 13 to LocalDate.of month value will cause an error? 有谁知道我将13传递给LocalDate.of月值的假设是否会导致错误? If so is there a way to do what I want to do that doesn't look so bad and uses a build in method? 如果是这样有一种方法可以做我想做的事情,看起来不那么糟糕,并使用构建方法?

Fortunately, Java makes this really easy with the idea of adjusters and TemporalAdjusters.firstDayOfNextMonth() : 幸运的是,Java使调整器和TemporalAdjusters.firstDayOfNextMonth()的想法变得非常简单:

import java.time.*;
import java.time.temporal.*;

public class Test {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2018, 12, 3);
        LocalDate date2 = date1.with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.println(date2); // 2019-01-01
    }
}
  1. Custom way with : 定制方式:

    • .plusMonths(1) to get the next month .plusMonths(1)获得下个月
    • .withDayOfMonth(1) to get the first day .withDayOfMonth(1)获得第一天

       LocalDate localDate = LocalDate.of(2018, 12, 15); LocalDate firstNext = localDate.plusMonths(1).withDayOfMonth(1); // or firstNext = localDate.withDayOfMonth(1).plusMonths(1); System.out.println(firstNext); //2019-01-01 

  1. Built-in way with : 内置方式:

    • TemporalAdjusters.firstDayOfNextMonth()

       firstNext = localDate.with(TemporalAdjusters.firstDayOfNextMonth()); // does a temporal.with(DAY_OF_MONTH, 1).plus(1, MONTHS); operation 

Using LocalDate you can get firstDayofNextMonth with TemporalAdjusters.firstDayOfNextMonth() 使用LocalDate您可以使用TemporalAdjusters.firstDayOfNextMonth()获取firstDayofNextMonth

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Test {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        LocalDate firstDayOfNextMonth = date.with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.println(firstDayOfNextMonth);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM