ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA/자바] 달력 ( Calendar ) 만들기
    Language/Java 2018. 3. 17. 19:04
    반응형

    달력 Calendar 

    만들고 싶은 기능

    1. 실제 달력처럼 년도와 월이 확인 가능해야 한다.

    2. 재시도 기능



    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
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    import java.io.*;
    import java.util.*;
     
    class MyCalendar{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        int yearInt;
        int monthInt;
        
        MyCalendar(){
            input();
            showCalendar();
            replay();
        }
     
        void input(){
            System.out.println("보고싶은 년도와 월을 입력해주세요.");
            System.out.print("예시 :: XXXX XX\n입력 >> ");
            try{
                line = br.readLine();
                line = line.trim();
                if(line.indexOf(" "!= -1){
                    cut(line);
                }else{
                    System.out.println("예시처럼 입력해주세요");
                    input();
                }
            }catch(IOException ioe){
            }catch(NumberFormatException nfe){
                System.out.println("제대로 입력하였는지 확인해주세요.\n");
                input();
            }
        }
     
        void cut(String str){
            int idx = str.indexOf(" ");
            String yearStr  = str.substring(0 , idx);
            String monthStr = str.substring(idx + 1);
            monthStr = monthStr.trim();
            yearInt  = Integer.parseInt(yearStr);
            monthInt = Integer.parseInt(monthStr);
     
            if(yearInt < 1900){
                System.out.println("자바는 1900년 이전을 보여주지 않습니다.");
                System.out.println("처음 화면으로 돌아갑니다.\n");
                input();
            }
            if(monthInt>12 || monthInt<1){
                System.out.println(">> 1 ~ 12 월 사이의 숫자를 입력해주세요.\n");
                input();
            }
        }
     
        void showCalendar(){
            Date date = new Date();
            Date useDate = new Date(yearInt-1900 , monthInt-1 , 1);
            date.setYear(yearInt);
            date.setMonth(monthInt);
            date.setDate(0);
            // API set 자동으로 변환 / 31일을 오버해버리면 자동으로 1일로 변환시켜준다.
            int dateI = date.getDate();
            int yearI = useDate.getYear();
            int monthI = useDate.getMonth();
            int dayI = useDate.getDay();
            int count = 1;
            System.out.println("\n\n\t[\t\t\t" + (yearI+1900+ "년\t" + (monthI+1+ "월\t\t\t]\n");
            System.out.println("\tSUN\tMON\tTUE\tWND\tTHU\tFRI\tSAT\t");
            for(int i=0 ; i<dateI ; i++){
                for(int j=0 ; j<8 ; j++){
                    if(j == 7){
                        System.out.println("\n\n");
                    }else if(count > dateI){
                        return;
                    }else if(dayI == j){
                        System.out.print("\t" + count);
                        count++;
                        dayI++;
                        if(dayI == 7){
                            dayI = 0;
                        }
                    }else{
                        System.out.print("\t");
                    }
                }
            }
        }
     
        void replay(){
            System.out.println("\n\n\n   ┌ 다시 하시겠습니까?  (y/n) ┐\n   │ 아무키를 누르면 종료    │\n   └───────────────┘");
            System.out.println("   │ y를 누르면 다시        │");
            System.out.print("   >> ");
            try{
                line = br.readLine();
                line = line.trim();
                if(line.equalsIgnoreCase("y")){
                    new MyCalendar();
                }else{
                    System.exit(0);
                }
            }catch(IOException ioe){}
        }
     
        public static void main(String[] args){
            MyCalendar mc = new MyCalendar();
        }
    }
    cs






    윤달에 대해서는 자동으로 잡아주기 때문에 신경쓰지 않아도 된다.

    \n 은 엔터를 친것과 같이 줄 내림이다.

    \t 는 탭만큼 거리를 뛰운다.



    반응형
Designed by Tistory.