본문 바로가기

명사 美 비격식 (무리 중에서) 아주 뛰어난[눈에 띄는] 사람[것]

JAVA

TemporalAdjusters, 자주쓰일만한 날짜 계산을 대신 해주다.

TemporalAdjusters

plus(), minus()와 같은 메서드로 날짜 와 시간을 계산할 수 있으나 지난주 토요일의 날짜, 이번달의 3번째 금요일의 날짜 등을 계산하기엔 어렵다.

자주쓰일만한 날짜 계산을 대신 해주는 메서드를 정의해놓은 것이 TemporalAdjusters클래스.

https://standout.tistory.com/1248

 

LocalDate와 LocalTime: get() getLong()특정필드값 가져오기, with() plus() minus() 변경하기, truncatedTo() 0으로

LocalDate와 LocalTime은 java.time 패키지의 가장 기본이 된다. 나머지 클래스들은 이들의 확장. https://standout.tistory.com/1245 java.time패키지: 객체생성 now() of() parse()와 인터페이스, 간격을 나타내는 Period Du

standout.tistory.com

 

firstDayOfNextYear()

다음 연도의 첫 번째 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate firstDayOfNextYear = date.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println(firstDayOfNextYear);
출력결과: 예를 들어, 현재 날짜가 2024-02-02인 경우, 출력 결과는 2025-01-01이 됩니다.

 

firstDayOfNextMonth()

다음 월의 첫 번째 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate firstDayOfNextMonth = date.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println(firstDayOfNextMonth);

 

firstDayOfYear()

현재 연도의 첫 번째 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate firstDayOfYear = date.with(TemporalAdjusters.firstDayOfYear());
System.out.println(firstDayOfYear);

 

firstDayOfMonth()

현재 월의 첫 번째 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate firstDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(firstDayOfMonth);

 

firstDayOfYear()

현재 연도의 첫 번째 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate firstDayOfYear = date.with(TemporalAdjusters.firstDayOfYear());
System.out.println(firstDayOfYear);

 

firstDayOfMonth()

현재 월의 첫 번째 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate firstDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(firstDayOfMonth);

 

firstInMonth(DayOfWeek dayOfWeek)

현재 월에서 첫 번째로 나타나는 지정된 요일의 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate firstMondayOfMonth = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(firstMondayOfMonth);

 

lastInMonth (DayOfWeek dayOfWeek)

현재 월에서 마지막으로 나타나는 지정된 요일의 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate lastMondayOfMonth = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));
System.out.println(lastMondayOfMonth);

 

previous (DayOfWeek dayOfWeek)

이전에 나타난 지정된 요일의 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate previousMonday = date.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println(previousMonday);

 

previousOrSame (DayOfWeek dayOfWeek)

이전에 나타난 지정된 요일의 날짜를 반환하거나, 주어진 날짜와 같은 요일이 있는 경우 주어진 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate previousOrSameMonday = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
System.out.println(previousOrSameMonday);

 

next (DayOfWeek dayOfWeek)

다음에 나타날 지정된 요일의 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate nextMonday = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(nextMonday);

 

nextOrSame (DayOfWeek dayOfWeek)

다음에 나타날 지정된 요일의 날짜를 반환하거나, 주어진 날짜와 같은 요일이 있는 경우 주어진 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate nextOrSameMonday = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
System.out.println(nextOrSameMonday);

 

dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)

월에서 주어진 순서에 해당하는 지정된 요일의 날짜를 반환

LocalDate date = LocalDate.now();
LocalDate secondWednesdayOfMonth = date.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.WEDNESDAY));
System.out.println(secondWednesdayOfMonth);