Java Certification, Programming, JavaBean and Object Oriented Reference Books
Class to read in dates and determine if they are valid and produce outputs

classtoreadindatesanddetermineiftheyarevalidandproduceoutputs.java

/*
  class to read in dates and determine if they are valid and produce outputs
*/

public class newDate

{
 
 /* sets up an array wih all the months stored in , for retrieval within the program */
 public final static String [] Month = {" "," January "," February "," March ", " April "," May "," June "," July "," August "," September "," October "," November "," December "};
 
 /* sets up an array with the days for retreival within the program */
 public final static String [] Day = {" Sunday "," Monday "," Tuesday "," Wednesday "," Thursday "," Friday "," Saturday "};
 
 /* sets up an array with the valus of the dates. Mostly used within the julian calendar */
 
 public final static int [] Dates1 = {0,31,28,31,30,31,30,31,31,30,31,30,31};
 public final static int [] Dates2 = {0,31,29,31,30,31,30,31,31,30,31,30,31};
 
 /* sets up the variables to be used within the newDate class */
 public int day;
 public int month;
 public int year;
 public int day1;
 public int day2;
 public int month1;
 public int month2;
 public int year1;
 public int year2;
 
 /* values to be used as temp storage */
 public int tempday = 0; /* used in the julian calender */
 public int tempmonth = 0; /* used in the julian calender */
 public int tempday2 = 0; /* used in the Datename string */
 public double dtemp;
 public int d;
 public int M;
 public int D;
 public int C;
 public int Y; /* used in Zellars formula */
 public int yeargap, yeargap1, yeargap2; /* used in the days elasped to check for leap years */
 public int tempyear = 0; /* used in the leapyear */
 public int tempyear1;
 public String tempyear2; /* used in the DateName String */
 public String Julian1;
 public String JulianDate1, JulianDate2; /* used to output the Julian date for each date */
 public String wholedate; // used to produce the complete date with the day included
 public String leap1; // used to display if it's a leap year or not
 public int totallapse;
 public int timelapse1;
 public int timelapse2; /* used to calculate the numbers of days between */
 public String timelapse; /* used to output the total number of days between two dates */
 public String subyear;
 public String days1, days2;
 

 /* Constructors */
 
 public newDate(){}   /* defaullt constructor */
 
 
 public newDate(int day1 , int month1 , int year1 , int day2, int month2 , int year2 )
 {
  setDaysElapsed(day1,month1,year1,day2,month2,year2);
 }
 
 
 /* Methods */
 
 
 
 public void setDaysElapsed(int day1, int month1, int year1, int day2 , int month2, int year2)
 {
  day = day1;
  month = month1;
  year = year1;
  if (validDate(day,month, year))
  {
   dateName(day, month,year);
   days1 = wholedate;
   julianDay(day,month,year);
   JulianDate1 = Julian1;
   timelapse1 = tempday + year1;
  }
  else
  {
   System.out.println("An incorrect date has been set ");
  }
 
  day = day2;
  month = month2;
  year = year2;
  if (validDate(day,month, year))
  {
   dateName(day, month,year);
   days2 = wholedate;
   julianDay(day,month,year);
   JulianDate2 = Julian1;
   timelapse2 = tempday + year1;
  }
  else
  {
   System.out.println("An incorrect date has been set ");
  }
 
 
  if (timelapse1<=timelapse2) // checks if the first date is earlier than the secobd date
  {
   if (year1 < year2) // checks to see if there is a gap in the years
   {
    yeargap = year2 - year1;// used to identify if a leap year exists between two dates
    yeargap2 = yeargap1 / 4;
    yeargap1 = ((yeargap-yeargap2) * 365)+(yeargap2*366);
   }
   else
   {
    yeargap1 = 0;
   }
   totallapse = 0;
   totallapse = (timelapse2 - timelapse1) + yeargap1;
   timelapse = ("The number of days between the first date and the second is " + totallapse  + " days");
  }
  else
  {
   timelapse = ("The first date was greater than the second date");
  }
 
 
 }// ends daysElapsed
 
 
 
 public boolean validDate ( int day, int month, int year )
 {
  if (((day<0)||(day>31)||((month<1)||(month>12))))
  {
   return false;
  }
  else if (((day<0)||(day>30))||(month==4)||(month==6)||(month==9)||(month==11))
  {
   return false;
  }
 
  else if (((day >= 1) || (day <=29)) && ( month == 2) && ( tempyear == 0)) // tempyear set to zero if the year is a leap year
  {
   return true;
  }
 
  else if (((day >=1) || (day <=28)) && ( month == 2) && ( tempyear == 1)) // tempyear set to one if the year is not a leap year
  {
   return true;
  }

  else if (year<0)
  {
   return false;
  }
  else
  {
   return true;
  }
 
 } // ends ValidDate
 
 
 
 public boolean leapYear (int year)
 {
 
  if (((year%4)==0)&&((year%100)!=0))
  {
   tempyear = 0;
   return true;
  }
  else if ((year%400)==0)
  {
   tempyear = 0;
   return true;
  }
  else
  {
   tempyear = 1;
   return false;
  }
 } // ends leap year
 
 
 
 public void julianDay(int day,int month,int year)
 {
 
  if (validDate(day, month, year))
  {
   if(leapYear(year))
   {
 
    leap1 = (" A leap year has been entered ");
    System.out.println("The year " + year + " is leap year ");
   }
   else
   {
 
    leap1 = (" The year entered is not a leap year ");
    System.out.println("The year " + year + " is not a leap year ");
   }
 
   if (tempyear ==1)
   {
    tempmonth = 0;
    for (int i=0;i<month;i++)
    {
     tempmonth =  tempmonth + (Dates1[i]);
    }
 
    tempday = day + tempmonth;
    Julian1 = ("Julian date is " + tempday + " of year " + year);
   }
   else
   {
    tempmonth = 0;
    for (int i=0; i<month;i++)
    {
     tempmonth = tempmonth + (Dates2[i]);
    }
 
    tempday = day + tempmonth;
    Julian1 = ("Julian date is " + tempday + " of year " + year);
   }
  }
  else
  {
   System.out.println ("The date entered was incorrect");
  }
 
 }// ends Julian
 
 
 
 public void dateName (int day, int month,int year)
 {
  if (validDate(day,month,year))
  {
   D = day;
   if (month < 3)
   {
    M = month +10;
    Y = (year - 1)%100;
    C = (year -1 )/100;
   }
   else
   {
    M = month - 2;
    Y = year%100;
    C = year/100;
   }
 
   int d = (700 + (( (26*M)-2)/10) + D + Y + Y/4 + C/4 - (2*C))%7;
 
   wholedate = ( Day[d] + day + Month[month] + year);
 
 
 
 
  }
  else
  {
   System.out.println("An incorrect date has been entered");
  }
 } // ends dateName
 
 
 
 /* get methods  */
 
 public String getLeapYear()
 {
  return leap1;
 }
 
 public String getJulianDay1()
 {
  return JulianDate1;
 }
 
 public String getJulianDay2()
 {
  return JulianDate2;
 }
 
 public String getDateName1()
 {
  return days1;
 }
 
 public String getDateName2()
 {
  return days2;
 }
 
 public String getDaysElapsed()
 {
  return timelapse;
 }
 
} /* ends the class */

Return to : Java Programming Hints and Tips