Read the Java online API documentation for the split method
Solution
import java.util.*;
public class Date {
private int day; // days
private int month; //months 1 to 12
private int year; // year
// week day names
private static final String[] DAYNAMES = {\"Sunday\", \"Monday\", \"Tuesday\",
\"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"};
// year month names
private static final String[] MONTHNAMES = {\"\", \"January\", \"February\",
\"March\", \"April\", \"May\", \"June\", \"July\", \"August\",
\"September\", \"October\", \"November\", \"December\"};
// month day dates
private static final int[] MONTHDAYS = {0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
// supporting date upto 1582 year calender
private static final int FIRST_MONTH = 10;
private static final int FIRST_DAY = 15;
private static final int FIRST_YEAR = 1582;
private static final Date FIRST_DATE = new Date(FIRST_MONTH, FIRST_DAY, FIRST_YEAR);
private static final int START_DAY_INDEX = 5;
public Date (Date date){
this.month = date.month;
this.day = date.day;
this.year = date.year;
}
public Date(int month, int day, int year) {
String s = month + \"/\" + day + \"/\" + year;
// check for correct/wrong month
if (month > 12 || month < 1) {
throw new DateException(s + \"\ Illegal month: \" + month
+ \" (Month must be between 1 and 12)\");
}
// check for year correct or not
if ((year < 1582)
|| (year == 1582 && month < 10)
|| (year == 1582 && month == 10 && day < 15)) {
throw new DateException(s
+ \" (Sorry we are not supporting this year as of now.)\");
}
// check for leap year
if (month == 2 && day == 29 && !isLeapYear(year)) {
throw new DateException(s + \" (\" + year + \" is not a leap year)\");
}
// check given month is legal or not
if ((day <= 0)
|| (month == 2 && isLeapYear(year) && day > 29)
|| (month == 2 && !isLeapYear(year) && day > 28)
|| (month != 2 && day > MONTHDAYS[month])) {
throw new DateException(s + \"\ Illegal day: \" + day + \"\ For \"
+ (month == 2 ? \"February \" + year : MONTHNAMES[month])
+ \", must be between 1 and \"
+ (isLeapYear(year) && month == 2 ? 29
: MONTHDAYS[month]));
}
// here if month, day, and year are all good
this.month = month;
this.day = day;
this.year = year;
}
public void setDate(int month, int day, int year){
this.month = month;
this.day = day;
this.year = year;
}
// getter day method
public int getDay() {
return day;
}
// getter month method
public int getMonth() {
return month;
}
// getter int method
public int getYear() {
return year;
}
// getter month method
public String getMonthName() {
return MONTHNAMES[month];
}
// return day week
public String getDayOfWeek() {
// create sample temp date
Date tempDate = new Date(FIRST_MONTH, FIRST_DAY, FIRST_YEAR);
int dayNameIndex = START_DAY_INDEX;
// check until match found
while (!tempDate.equals(this)) {
tempDate.next();
dayNameIndex = (dayNameIndex + 1) % 7;
}
return DAYNAMES[dayNameIndex];
}
// get short date
public String getShortDate() {
String out = \"\";
if (month <= 9) // add leading zero if single digit month
{
out += \"0\";
}
out += month + \"/\";
if (day <= 9) // add leading zero if single digit day
{
out += \"0\";
}
return out + day + \"/\" + year;
}
// get simple dare
public String getMediumDate() {
return MONTHNAMES[month] + \" \" + day + \", \" + year;
}
// det date in string format
public String getLongDate() {
String out = this.getDayOfWeek() + \", \" + MONTHNAMES[month]
+ \" \" + day;
switch (day) {
case 1:
out += \"st, \";
break;
case 2:
out += \"nd, \";
break;
case 3:
out += \"rd, \";
break;
case 21:
out += \"st, \";
break;
case 22:
out += \"nd, \";
break;
case 23:
out += \"rd, \";
break;
case 31:
out += \"st, \";
break;
default:
out += \"th, \";
}
return out + year;
}
// check leapyear
private boolean isLeapYear() {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
// check specific year is leap year or not
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static void main(String args[]){
//create scanner object
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter first day of class --> \");
String firstDay = sc.nextLine();
System.out.println(\"Enter last day of class --> \");
String lastDay = sc.nextLine();
// psrase string
String[] firstDayArr = firstDay.split(\"/\");
String[] lastDayArr = lastDay.split(\"/\");
//create date objects
Date date1 = new Date(Integer.parseInt(firstDayArr[0]),Integer.parseInt(firstDayArr[1]),Integer.parseInt(firstDayArr[2]));
Date date2 = new Date(Integer.parseInt(lastDayArr[0]),Integer.parseInt(lastDayArr[1]),Integer.parseInt(lastDayArr[2]));
// printing results
System.out.println(\"First day of class: \"+date1.getLongDate());
System.out.println(\"Last day of class: \"+date2.getLongDate());
}
}



