viewing paste DisplayCalendar.java | Java

Posted on the
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
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();
        
        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,i;
        init = firstDay;
        
        for(n = 0; n < 12; n++){
            result[n] = " ";
            
            for(i = 0; i < init%7; i++)
                result[n] += "     ";
            
            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++;
                        
                        result[n] += day;
                        if(day <= 7)
                            result[n] += "     ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                } else {
                    for(day = 1; day <= 30; day++){
                        init++;
        
                        result[n] += day;
                        if(day <= 7)
                            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++;
                        
                        result[n] += day;
                        if(day <= 7)
                            result[n] += "     ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                }else if(year%400 == 0){
                    for(day = 1; day <= 29; day++){
                        init++;
                        
                        result[n] += day;
                        if(day <= 7)
                            result[n] += "     ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                }else{
                    for(day = 1; day <= 28; day++){
                        init++;
                        
                        result[n] += day;
                        if(day <= 7)
                            result[n] += "     ";
                        else
                            result[n] += "    ";
                        
                        if(init%7 == 0)
                            result[n] += "\n";
                    }
                }
            }
            
        }
        
        return result;
        
    }
}
Viewed 1332 times, submitted by Streusel.