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());
Looking at the doc YearMonth api, try:
YearMonth myYearMonth = YearMonth.from(localDate);
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With