[java] 월의 주, 주의 처음과 마지막 일자 구하기 (Canlendar 클래스)

Jinbro·2023년 2월 14일
0

Java

목록 보기
6/7

TODO

  • week 관련 날짜 계산 PoC
    • week 기준 요일
      • 시작 : 일요일
      • 종료 : 토요일
  1. 월의 주
    • week of month
      ex 1) 2023.02.11 (토) -> 2월 2주차
      ex 2) 2023.02.12 (일) -> 2월 3주차
  2. 주의 처음과 마지막 날짜
    • first and last day of week
      ex) today : 2023.02.15 (수)
      => first day of week : 2023.02.12 (일)
      => last day of week : 2023.02.18 (토)

해결방안

  • java.util.Calendar 클래스 활용

제약사항

  • jdk 1.7

테스트 (JUnit)

  • 월의 주 반환
  • 주의 처음과 마지막 일자 반환
@Test
@DisplayName("Calendar 클래스 week 테스트")
void calendarClassWeekTest() throws ParseException {
	int y = 2023;
	int m = Calendar.FEBRUARY;
	int d = 15;
		
	String format = "yyyy-MM-dd (EEE)";
	SimpleDateFormat sdf = new SimpleDateFormat(format);
		
	Calendar c = Calendar.getInstance();
	c.set(Calendar.YEAR, y);
	c.set(Calendar.MONTH, m);
	c.set(Calendar.DAY_OF_MONTH, d);
		
	System.out.println("m월 n주차 : " + ( c.get(Calendar.MONTH) + 1 ) + "월 " + c.get(Calendar.WEEK_OF_MONTH) + "주차");
		
	System.out.println("today : " + sdf.format(c.getTime()));
		
	c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
	System.out.println("first day of week : " + sdf.format(c.getTime()));
		
	c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
	System.out.println("last day of week : " + sdf.format(c.getTime()));
}
  • console
m월 n주차 : 23주차
today : 2023-02-15 ()
first day of week : 2023-02-12 ()
last day of week : 2023-02-18 ()
profile
자기 개발 기록 저장소

0개의 댓글