Advertisement
brilliant_moves

DateConverter.java

Nov 12th, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.40 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class MonthException extends Exception {
  4.     public MonthException() {
  5.         System.out.println("Error in month number.");
  6.         System.out.println("Please re-enter date.");
  7.         System.exit(0);
  8.     }
  9. }
  10.  
  11. class DayException extends Exception {
  12.     public DayException() {
  13.         System.out.println("Error in day number.");
  14.         System.out.println("Please re-enter date.");
  15.         System.exit(0);
  16.     }
  17. }
  18.  
  19. class YearException extends Exception {
  20.     public YearException() {
  21.         System.out.println("Error in year number.");
  22.         System.out.println("Please re-enter date.");
  23.         System.exit(0);
  24.     }
  25. }
  26.  
  27. public class DateConverter {
  28.  
  29.     /**
  30.     *   Program:    DateConverter.java
  31.     *   Purpose:    Yahoo! Answers
  32.     *   Creator:    Chris Clarke
  33.     *   Created:    12.11.2014
  34.     */
  35.  
  36.     public static boolean isLeapYear(int year) {
  37.          return (year%4 == 0 && (year%100 != 0 || year%400 == 0));
  38.     } // isLeapYear()
  39.  
  40.     public static String getMonthName(int m) {
  41.         switch (m) {
  42.             case 1:     return "January";
  43.             case 2:     return "February";
  44.             case 3:     return "March";
  45.             case 4:     return "April";
  46.             case 5:     return "May";
  47.             case 6:     return "June";
  48.             case 7:     return "July";
  49.             case 8:     return "August";
  50.             case 9:     return "September";
  51.             case 10:    return "October";
  52.             case 11:    return "November";
  53.             case 12:    return "December";
  54.         } // switch
  55.         return "Error in month name!";
  56.     } // getMonthName()
  57.  
  58.     public static void main(String[] args) throws MonthException, DayException, YearException {
  59.         Scanner in  = new Scanner(System.in);
  60.         String date;
  61.         String[] d;
  62.         String monthName;
  63.         int month, day, year;
  64.         int daysInMonth;
  65.  
  66.         System.out.print("Enter date <mm/dd/yyyy> : ");
  67.         date = in.nextLine();
  68.         d = date.split("/");
  69.  
  70.         month = Integer.parseInt(d[0]);
  71.         day = Integer.parseInt(d[1]);
  72.         year = Integer.parseInt(d[2]);
  73.  
  74.         if (month<1 || month>12) {
  75.             throw new MonthException();
  76.         } // if
  77.  
  78.         if (month==2) {
  79.             if (isLeapYear(year))
  80.                 daysInMonth = 29;
  81.             else
  82.                 daysInMonth = 28;
  83.         } else if (month==1 || month==3 || month==5 || month==7
  84.          || month==8 || month==10 || month==12) {
  85.             daysInMonth = 31;
  86.         } else {
  87.             daysInMonth = 30;
  88.         } // if
  89.  
  90.         if (day<1 || day>daysInMonth) {
  91.             throw new DayException();
  92.         } // if
  93.  
  94.         if (year<1000 || year>3000) {
  95.             throw new YearException();
  96.         } // if
  97.  
  98.         monthName = getMonthName(month);
  99.         System.out.println(monthName+" "+day+", "+year);
  100.     } // main()
  101.  
  102. } // class DateConverter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement