public class Date
{
/* Let's do this thing properly and set up a look-up table
* which will be used in to return month as string bit at
* the end. We'll store the months in an array of type
* String and as computers count from zero, our first entry
* will be monthToString[0], returning January. */
public static String [] monthToString=
{
"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"
};
/* Because I'm doing what should be advanced programming,
* I'm going to use a look-up table for the number of days
* in each month as follows: */
public static int [] noOfDays=
{
31,28,31,30,31,30,31,31,30,31,30,31
};
/* Here is a variable of type char that'll force a line-feed
* when using System.out.print ("text"+ln+"more text"); */
public static char ln=13;
/* The following variable of type String is also used in
* the sub-routine that returns the month as string */
public static String mon="";
/* Okay, here's some variables of type integer (whole numbers)
* which will be used in the constructor thingy */
private int day, month, year;
/* Error catching for type date in DD/MM/YY format to
* check for legal dates, assuming a 19xx or 20xx prefix*/
public Date (int dd, int mm, int yy)
{
day=dd; month=mm; year=yy;
if (month<1 || month>12)
{
System.out.println("?Month not valid error"+ln+
"Reset to default value.");
month=01;
}
if (day<1 || day>31)
{
System.out.println("?Day not valid error"+ln+
"Reset to default value.");
day=01;
}
/* As there is no easy way to store a leap-year in a look-up table
* I'm going to check for the 29th February first and validate it
* later. Of course, this is advanced programming, isn't it? */
if (day>29 && month==2)
{
System.out.println("?Day not valid error"+ln+
"Except for leap-years, Feburary"+
"has only 28 days.");
day=01;
}
/* Now, let's check to see if a legal leap-year has been entered
* by the user, shall we? Oh, we need to do some maths. */
if (day==29 && month==2 && ((year-2000)%4!=0))
{
System.out.println("?Date not valid error"+ln+"Not a leap-year.");
day=28;
}
/* So, we've tested for a valid leap-year, the only thing that could
* trip up our array above for the numbers of days in each month. */
if (day<1 || day>noOfDays[month-1] && month!=2)
{
System.out.println("?Date not valid error"+ln+"Not a legal date.");
day=01;
}
/* That's a whole lot more efficient and advanced than having a
* 'switch case' or a load of 'if' statements checking for each
* condition, isn't it? Okay, I'd use a switch case if I thought
* that the person maintaining the code wasn't very good and needed
* it to be readable, but if you use comments correctly then you
* don't necessarily need code to be more readable, or shouldn't */
}
/* This will initialise the date and then we'll do some stuff
* wid it */
public Date()
{
day=04;month=05;year=77;
}
/* This does something, but I can't remember what? */
public Date (Date other)
{
this (other.day, other.month, other.year);
}
/* This method will return the monthAsString, as suggested by its' name */
public String monthAsString()
{
/* This is very efficient because it has in-built error checking
* actually without testing for loads of conditions */
if (month>0 && month<13)
{
mon=monthToString[month-1];
return mon;
}
/* Just in case any errors have sneaked into validating the month,
* It will return a Month not set message */
else
return "Month not set";
}
/* I'm probably wasting my time trying to make this method
* more efficient. Not sure what benefits it has anyway? */
public boolean earlierThan(Date other)
{
if(year<other.year)
{
return true;
}
else
if (year==other.year && month<other.month)
{
return true;
}
else
if (month==other.month && day<other.day)
{
return true;
}
else
return false;
}
/* This does some sort of test for some reason, again I don't
* recall what it's actually for? */
public boolean equals (Date other)
{
if (day==other.day && month==other.month && year==other.year)
{
return true;
}
else
return false;
}
public String suffix()
{
/* Here is where a switch case is useful, in terms of returning a
* date: because most days as numbers have a th suffix (ie, 4th),
* we'll use a switch case for the exceptions to that rule. */
switch(day)
{
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
case 21: return "st";
case 22: return "nd";
case 23: return "rd";
case 31: return "st";
default: return "th";
}
}
public String toString()
{
/* This is another attempt at error trapping, just in case
* anything above has failled. */
if(month!=0 || day!=0)
{
if(year<10)
{
/* A nested if statement to do some padding on years less than xx10 */
return "The "+day+suffix()+" of "+monthAsString()+" in the year 0"+year;
}
else
return "The "+day+suffix()+" of "+monthAsString()+" in the year "+year;
}
else
return "Date not set";
}
public void copy(Date other)
/* I think this final thing is the equivalent of a buffer perhaps? */
{
day=other.day; month=other.month; year=other.year;
}
}