viewing paste CalculateDates | 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
import java.util.Scanner;
 
public  class   CalculateDates{
 
    public  static  void    main(String[] args){
        
        Scanner input = new Scanner(System.in);
        
        System.out.print("Please enter a year (E.g.: 2013): ");
        int year = input.nextInt();
        System.out.print("Please enter a month (1 = January, 12 = December): ");
        int month = input.nextInt();
        System.out.print("Please enter a day (1-31): ");
        int day = input.nextInt();
        
        validateInput(year,month,day);
        
    }
    public  static  void    validateInput(int year, int month, int day){
        if(isLeapYear(year) && month == 2 && day > 29) {
            System.out.println("Invalid input.");
            System.out.println("Aborting..");
            System.exit(0);
        } else if(isLeapYear(year) == false && month == 2 && day > 28) {
            System.out.println("Invalid input.");
            System.out.println("Aborting..");
            System.exit(0);
        } else if(month > 12 || month == 1 && day > 31 || month == 3 && day > 31 || month == 4 && day > 30 || month == 5 && day > 31 || month == 6 && day > 30 || month == 7 && day > 31 || month == 8 && day > 31 || month == 9 && day > 30 || month == 10 && day > 31 || month == 11 && day > 30 || month == 12 && day > 31) {
            System.out.println("Invalid input.");
            System.out.println("Aborting..");
            System.exit(0);
        }
        daysInMonth(month,year);
        getMonthName(month);
        
        System.out.println(computeTotalDays(year,month,day)+" days have passed since the beginning of the year.");
        System.out.print("The next day is ");
        nextDay(year,month,day);
        
    return;
    }
    public  static  String  getMonthName(int month){
        String month_name = new String();
        switch(month){
            case 1:     month_name = "January"; break;
            case 2:     month_name = "February"; break;
            case 3:     month_name = "March"; break;
            case 4:     month_name = "April"; break;
            case 5:     month_name = "May"; break;
            case 6:     month_name = "June"; break;
            case 7:     month_name = "July"; break;
            case 8:     month_name = "August"; break;
            case 9:     month_name = "September"; break;
            case 10:    month_name = "October"; break;
            case 11:    month_name = "November"; break;
            case 12:    month_name = "December"; break;
            default:    month_name = "January"; break;
        }
    return month_name;
    }
    public  static  boolean isLeapYear(int year){
        boolean day;
        if(year%4 == 0 && year%100 != 0){
            day = true;
        }else if(year%400 == 0){
            day = true;
        }else{
            day = false;                
        }
    return day;
    }
    public  static  int daysInMonth(int month, int year){
        int day = 0;
        int i;
        for(i = 1; i < month; i++){
            switch(i){
                case 1:     day += 31; break;
                case 2:     if(isLeapYear(year)) day += 29; else day += 28; break;
                case 3:     day += 31; break;
                case 4:     day += 30; break;
                case 5:     day += 31; break;
                case 6:     day += 30; break;
                case 7:     day += 31; break;
                case 8:     day += 31; break;
                case 9:     day += 30; break;
                case 10:    day += 31; break;
                case 11:    day += 30; break;
                case 12:    day += 31; break;
                default:    System.out.println("Unknown Error.."); System.exit(0);
            }
        }
    return day;
    }
    public  static  int computeTotalDays(int year, int month, int day){
        
        int totaldays = day;
        
        totaldays += daysInMonth(month,year);
        
        return totaldays;
    }
    public  static  void    nextDay(int year, int month, int day){
        
        int nextday = 0;
        int daysinmonth = 0;
        
        switch(month){
            case 1:     daysinmonth = 31; break;
            case 2:     if(isLeapYear(year)) daysinmonth = 29; else daysinmonth = 28; break;
            case 3:     daysinmonth = 31; break;
            case 4:     daysinmonth = 30; break;
            case 5:     daysinmonth = 31; break;
            case 6:     daysinmonth = 30; break;
            case 7:     daysinmonth = 31; break;
            case 8:     daysinmonth = 31; break;
            case 9:     daysinmonth = 30; break;
            case 10:    daysinmonth = 31; break;
            case 11:    daysinmonth = 30; break;
            case 12:    daysinmonth = 31; break;
            default:    System.out.println("Unknown Error.."); System.exit(0);
        }
        
        if(month == 12 && (daysinmonth-day) == 0)
            year++;
        
        if((daysinmonth-day) == 0){
            month++;
            getMonthName(month);
            day = 1;
        }else{
            day++;
        }
        System.out.println(getMonthName(month)+" "+day+", "+year);
    }
}
Viewed 1578 times, submitted by Streusel.