Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.InputMismatchException;
- public class DateFormat
- {
- public static int monthNumber() throws MonthException
- {
- int month = 0;
- Scanner keyboard = new Scanner(System.in);
- try
- {
- System.out.println("Enter month number: ");
- month = keyboard.nextInt();
- if(month <= 0 || month > 12)
- throw new MonthException();
- }
- catch(InputMismatchException e)
- {
- throw new MonthException();
- }
- return month;
- }
- public static int dayNumber() throws DayException
- {
- int day = 0;
- Scanner keyboard = new Scanner(System.in);
- try
- {
- System.out.println("Enter day number: ");
- day = keyboard.nextInt();
- if(day > 31 || day <= 0)
- throw new DayException();
- }
- catch(InputMismatchException e)
- {
- throw new DayException();
- }
- return day;
- }
- public static int yearNumber() throws YearException
- {
- int year = 0;
- Scanner keyboard = new Scanner(System.in);
- try
- {
- System.out.println("Enter year number: ");
- year = keyboard.nextInt();
- if(year > 3000 || year <= 1000)
- throw new YearException();
- }
- catch(InputMismatchException e)
- {
- throw new YearException();
- }
- return year;
- }
- public static String numToString(int month)
- {
- switch(month)
- {
- 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";
- }
- }
- public class MonthException extends Exception
- {
- public MonthException()
- {
- super("Incorrect month");
- }
- public MonthException(String message)
- {
- super(message);
- }
- }
- public class DayException extends Exception
- {
- public DayException()
- {
- super("Invalid day.");
- }
- public DayException(String message)
- {
- super(message);
- }
- }
- public class YearException extends Exception
- {
- public YearException()
- {
- super("Invalid year.");
- }
- public YearException(String message)
- {
- super(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement