웹 개발

Get first and last day of month, LocalDate

노루아부지 2020. 12. 25. 12:47

특정 month의 1일과 마지막 날짜를 구하고 싶다면 아래와 같이 하면 됩니다.

 

방법 1.

LocalDate monthstart = LocalDate.of(year,month,1);
LocalDate monthend = monthstart.plusDays(monthstart.lengthOfMonth()-1);

 

방법 2. Java에서 제공하는 YearMonth class 사용

YearMonth month = YearMonth.from(date);
LocalDate start = month.atDay(1);
LocalDate end   = month.atEndOfMonth();

 

만약, 이번달의 1일과 마지막 날짜를 구하고 싶다면 아래와 같이 할 수 있습니다.

LocalDate start = YearMonth.now().atDay(1);
LocalDate end   = YearMonth.now().atEndOfMonth();

 

방법 3. withDayOfMonth, lengthOfMonth method 사용

LocalDate initial = LocalDate.of(2014, 2, 13);
LocalDate start = initial.withDayOfMonth(1);
LocalDate end = initial.withDayOfMonth(initial.lengthOfMonth());

 

 

 

[출처]

stackoverflow.com/questions/22223786/get-first-and-last-day-of-month-using-threeten-localdate

728x90
loading