Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to truncate date to Month with Java 8?

I want to get the milliseconds truncated to days, I can use

Instant.now().truncatedTo(ChronoUnit.DAYS).toEpochMilli() 

But I can't truncate to ChronoUnit.MONTH (it throws an exception). Do I need use a Calendar?

like image 378
EnverOsmanov Avatar asked Jun 11 '15 08:06

EnverOsmanov


People also ask

How to get only month from LocalDate in Java?

LocalDate getMonth() method in JavaThe getMonth() method of LocalDate class in Java gets the month-of-year field using the Month enum.

How to get month and year from Calendar in Java?

The getDayOfMonth() method returns the day represented by the given date, getMonth() method returns the month represented by the given date, and getYear() method returns the year represented by the given date.


1 Answers

This is what java.time.temporal.TemporalAdjusters are for.

date.with(TemporalAdjusters.firstDayOfMonth()).truncatedTo(ChronoUnit.DAYS); 
like image 124
Simon Avatar answered Sep 21 '22 19:09

Simon