Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class MonthException extends Exception {
- public MonthException() {
- System.out.println("Error in month number.");
- System.out.println("Please re-enter date.");
- System.exit(0);
- }
- }
- class DayException extends Exception {
- public DayException() {
- System.out.println("Error in day number.");
- System.out.println("Please re-enter date.");
- System.exit(0);
- }
- }
- class YearException extends Exception {
- public YearException() {
- System.out.println("Error in year number.");
- System.out.println("Please re-enter date.");
- System.exit(0);
- }
- }
- public class DateConverter {
- /**
- * Program: DateConverter.java
- * Purpose: Yahoo! Answers
- * Creator: Chris Clarke
- * Created: 12.11.2014
- */
- public static boolean isLeapYear(int year) {
- return (year%4 == 0 && (year%100 != 0 || year%400 == 0));
- } // isLeapYear()
- public static String getMonthName(int m) {
- switch (m) {
- case 1: return "January";
- case 2: return "February";
- case 3: return "March";
- case 4: return "April";
- case 5: return "May";
- case 6: return "June";
- case 7: return "July";
- case 8: return "August";
- case 9: return "September";
- case 10: return "October";
- case 11: return "November";
- case 12: return "December";
- } // switch
- return "Error in month name!";
- } // getMonthName()
- public static void main(String[] args) throws MonthException, DayException, YearException {
- Scanner in = new Scanner(System.in);
- String date;
- String[] d;
- String monthName;
- int month, day, year;
- int daysInMonth;
- System.out.print("Enter date <mm/dd/yyyy> : ");
- date = in.nextLine();
- d = date.split("/");
- month = Integer.parseInt(d[0]);
- day = Integer.parseInt(d[1]);
- year = Integer.parseInt(d[2]);
- if (month<1 || month>12) {
- throw new MonthException();
- } // if
- if (month==2) {
- if (isLeapYear(year))
- daysInMonth = 29;
- else
- daysInMonth = 28;
- } else if (month==1 || month==3 || month==5 || month==7
- || month==8 || month==10 || month==12) {
- daysInMonth = 31;
- } else {
- daysInMonth = 30;
- } // if
- if (day<1 || day>daysInMonth) {
- throw new DayException();
- } // if
- if (year<1000 || year>3000) {
- throw new YearException();
- } // if
- monthName = getMonthName(month);
- System.out.println(monthName+" "+day+", "+year);
- } // main()
- } // class DateConverter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement