Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발자입니다

[Java] 블로그 일기 형식 자동 출력 코딩 본문

Java

[Java] 블로그 일기 형식 자동 출력 코딩

끈기JK 2022. 10. 3. 10:32

 

4년 이상 코드를 쓸 경우 윤년을 고려해 코드 수정해야 할거 같다.

메서드: .substring 

라이브러리: LocalDate, DayOfWeek

 

package practice;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;

public class DiaryDate {

	public static void main(String[] args) {
		
		int thisYear, thisMonth, thisDay;
		String thisYearMonth;
		String WeekDay;
		int [] endDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		String [] WeekDays = {"월", "화", "수", "목", "금", "토", "일"};
		
		Scanner sc = new Scanner(System.in);
		System.out.print("년월입력(예: 2022년 10월 -> 2210) : ");
		thisYearMonth = sc.nextLine();
		thisYear = Integer.parseInt("20" + thisYearMonth.substring(0, 2));
		thisMonth= Integer.parseInt(thisYearMonth.substring(2));
		thisDay = endDays[thisMonth-1];
		
		for(int i=1; i<=thisDay; i++) {
			LocalDate date = LocalDate.of(thisYear, thisMonth, i);
			DayOfWeek dayOfWeek = date.getDayOfWeek();
			int numWeekDay = dayOfWeek.getValue();
			WeekDay = WeekDays[numWeekDay-1];
			System.out.printf("%d월%d일 %s", thisMonth, i, WeekDay);
			System.out.println();
			System.out.println("-내가 배운 교훈과 새로운 약속");
			if(numWeekDay == 7) {
				System.out.println();
				System.out.println("한 주 정리");
				System.out.println("-지난 나와 지금 나의 차이");
			}
			
			System.out.println();
		}
		System.out.println("한 달 정리");
		System.out.println("-지난 나와 지금 나의 차이");
		
	}

}