viewing paste DisplayCalendar.java | Java

Posted on the | Last edited on
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
import java.util.Scanner;
 
public  class   DisplayCalendar{
 
    public  static  void    main(String[] args){
        
        int n,i;
        String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};      
        Scanner scanner = new Scanner(System.in);
        
//      System.out.print("Please input a year: ");
//      int year = scanner.nextInt();
//      System.out.print("Please input the first day: ");
//      int firstDay = scanner.nextInt();
        int year = 2012;
        int firstDay = 1;
        String[] result = callday(firstDay,year);
        
        
        for(i = 0; i < 12; i++){
            
            System.out.println("         "+months[i]+" "+year);
            System.out.print("____________________________________\n");
            System.out.print("Sun  Mon   Tue  Wed  Thu   Fri  Sat\n");
            System.out.println(result[i]+"\n");
            
        }
 
 
    }
    public static String[] callday(int num1, int num2){
        
        String[] result = new String[12];
        int firstDay = num1;
        int year = num2;
        int n,day,init;
//      init = 0;
        
        for(n = 0; n < 12; n++){
            result[n] = " ";
            init = 0;
            
            if(n != 1){
                if(n == 0 || n == 2 || n == 4 || n == 6 || n == 7 || n == 9 || n == 11){
                    for(day = 1; day <= 31; day++){
                        init++;
                        
                        if(init == 8)
                            result[n] += " ";
                        if(init == 9)
                            result[n] += " ";
                        result[n] += day;
                        if(init >= 17 && init <= 20 || init >= 24 && init <= 27)
                            result[n] += "   ";
                        else if(init >= 1 && init < 3)
                            result[n] += "     ";
                        else if(init >= 10 && init <=  14)
                            result[n] += "   ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                } else {
                    for(day = 1; day <= 30; day++){
                        init++;
        
                        if(init == 8)
                            result[n] += " ";
                        if(init == 9)
                            result[n] += " ";
                        result[n] += day;
                        if(init >= 17 && init <= 20 || init >= 24 && init <= 27)
                            result[n] += "   ";
                        else if(init >= 1 && init < 3)
                            result[n] += "     ";
                        else if(init >= 10 && init <=  14)
                            result[n] += "   ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                }
            } else {
                if(year%4 == 0 && year%100 != 0){
                    for(day = 1; day <= 29; day++){
                        init++;
                        
                        if(init == 8)
                            result[n] += " ";
                        if(init == 9)
                            result[n] += " ";
                        result[n] += day;
                        if(init >= 17 && init <= 20 || init >= 24 && init <= 27)
                            result[n] += "   ";
                        else if(init >= 1 && init < 3)
                            result[n] += "     ";
                        else if(init >= 10 && init <=  14)
                            result[n] += "   ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                }else if(year%400 == 0){
                    for(day = 1; day <= 29; day++){
                        init++;
                        
                        if(init == 8)
                            result[n] += " ";
                        if(init == 9)
                            result[n] += " ";
                        result[n] += day;
                        if(init >= 17 && init <= 20 || init >= 24 && init <= 27)
                            result[n] += "   ";
                        else if(init >= 1 && init < 3)
                            result[n] += "     ";
                        else if(init >= 10 && init <=  14)
                            result[n] += "   ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                }else{
                    for(day = 1; day <= 28; day++){
                        init++;
                        
                        if(init == 8)
                            result[n] += " ";
                        if(init == 9)
                            result[n] += " ";
                        result[n] += day;
                        if(init >= 17 && init <= 20 || init >= 24 && init <= 27)
                            result[n] += "   ";
                        else if(init >= 1 && init < 3)
                            result[n] += "     ";
                        else if(init >= 10 && init <=  14)
                            result[n] += "   ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                }
            }
            
        }
        
        return result;
        
    }
}
Viewed 1317 times, submitted by Streusel.