简体   繁体   中英

JAVA: How do i construct a LocalDate with variables as arguments for said LocalDate

public Double income(int year,int month){
        Double soma = salary;
        LocalDate data = LocalDate.of(year,month);

This is a function that will compare the year and month inputed by the user with a LocalDate of another object. I'm trying to use (int year,int month) as the arguments for the constructor of the LocalDate.of but i can't seem to make it work, thoughts?

A LocalDate represents a year, month, and day-of-month. You omit the day-of-month, so determining a date is impossible.

YearMonth

To represent only a year with month, but no day-of-month, use the aptly-named class YearMonth .

YearMonth ym = YearMonth.of( y , m ) ;  // Specify the year and the month.

You can determine a date from a YearMonth object by specifying a day-of-month. Here we get the first day of the month:

LocalDate ld = ym.atDay( 1 ) ;  // Get the date of the first of that month in that year.

To compare one LocalDate object with another, use the isEqual , isBefore , and isAfter methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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