Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
like image 894
user1746050 Avatar asked Mar 18 '14 10:03

user1746050


2 Answers

Looking at the doc YearMonth api, try:

YearMonth myYearMonth = YearMonth.from(localDate);
like image 68
Squizer Avatar answered Sep 18 '22 16:09

Squizer


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())
like image 23
Martin Avatar answered Sep 20 '22 16:09

Martin