user1746050
user1746050

Reputation: 2575

Convert threeten LocalDate to YearMonth

I have a LocalDate which I need to convert to YearMonth. I am using ThreeTen API (Backport of JSR-310 to Java SE 7). Is there anyway I can do this?

I tried this in Joda-Time but in ThreeTen it is not offered.

LocalDate date;
new YearMonth(date.getYear(), date.getMonthOfYear());

Upvotes: 16

Views: 23686

Answers (2)

Martin
Martin

Reputation: 2942

These commands all result in equal YearMonth values:

YearMonth.from(localDate)

localDate.query(YearMonth.FROM) // ThreeTen
localDate.query(YearMonth::from) // JDK 8

YearMonth.of(localDate.getYear(), localDate.getMonth())

YearMonth.of(localDate.getYear(), localDate.getMonthValue())

Upvotes: 4

Squizer
Squizer

Reputation: 651

Looking at the doc YearMonth api, try:

YearMonth myYearMonth = YearMonth.from(localDate);

Upvotes: 50

Related Questions