Hier nun also der Quellcode der Day Klasse:
Code:
import java.util.*;
import java.io.*;
import java.lang.Math;
public class Day
{
private int day = 0;
private int month = 0;
private int year = 0;
/**
* Constructs a today's date
*/
public Day()
{
GregorianCalendar todaysDate = new GregorianCalendar();
year = todaysDate.get(Calendar.YEAR);
month = todaysDate.get(Calendar.MONTH) + 1;
day = todaysDate.get(Calendar.DAY_OF_MONTH);
}//constructor
/**
* Constructs a specific date
*
* @param yyyy year (full year, e.g., 1996,
* <i>not</i> starting from 1900)
* @param m month
* @param d day
* @exception IllegalArgumentException if yyyy m d not a
* valid date
*/
public Day(int yyyy, int m, int d)
throws IllegalArgumentException
{
year = yyyy;
month = m;
day = d;
if (!isValid())
throw new IllegalArgumentException();
}//constructor
/**
* Constructs a specific date from a String. The String must be in
* format (D)D.(M)M.YYYY
*
* @param a String representation of a Date
* @exception IllegalArgumentException if yyyy m d not a
* valid date
*/
public Day(String dString)
throws IllegalArgumentException
{
int index = 0;
int oldindex = 0;
try
{
index = dString.indexOf('.',oldindex);
day = Integer.parseInt( dString.substring(oldindex,index) );
oldindex = index+1;
index = dString.indexOf('.',oldindex);
month = Integer.parseInt( dString.substring(oldindex,index) );
oldindex = index +1;
year = Integer.parseInt( dString.substring(oldindex,dString.length() ) );
}
catch (IndexOutOfBoundsException e)
{
//do nothing, because IllegalArgumentException is thrown below instead
}
if (!isValid() || dString.substring(oldindex,dString.length()).length() != 4 )
throw new IllegalArgumentException();
}//constructor
/**
* Constructs a specific date from a String.
* If SQLTimestamp is false, the String must be in
* format (D)D.(M)M.YYYY
* If SQLTimestamp is true, the String must be in
* format YYYY-MM-DD HH.MM.SS
* The Daytime information will be ignored then
*
* @param a String representation of a Date
* @param format of the Date string
* @exception IllegalArgumentException if yyyy m d not a
* valid date
*/
public Day(String dString, boolean SQLTimestamp)
throws IllegalArgumentException
{
int index = 0;
int oldindex = 0;
try
{
if (SQLTimestamp) { //if dString is a timestamp, rewrite it to DD.MM.YYYY
String newstr="";
newstr+=dString.substring(8,10) +".";
newstr+=dString.substring(5,7) +".";
newstr+=dString.substring(0,4);
dString=newstr; //assign dString the value of the rewritten date string
}
index = dString.indexOf('.',oldindex);
day = Integer.parseInt( dString.substring(oldindex,index) );
oldindex = index+1;
index = dString.indexOf('.',oldindex);
month = Integer.parseInt( dString.substring(oldindex,index) );
oldindex = index +1;
year = Integer.parseInt( dString.substring(oldindex,dString.length() ) );
}
catch (IndexOutOfBoundsException e)
{
//do nothing, because IllegalArgumentException is thrown below instead
}
if (!isValid() || dString.substring(oldindex,dString.length()).length() != 4 )
throw new IllegalArgumentException();
}//constructor
/**
* Gets the day of the month
*
* @return the day of the month (1...31)
*
*/
public int getDay()
{
return day;
}//getDay
/**
* Gets the month
*
* @return the month (1...12)
*
*/
public int getMonth()
{
return month;
}//getMonth
/**
* Gets the year
*
* @return the year (counting from 0, <i>not</i> from 1900)
*
*/
public int getYear()
{
return year;
}//getYear
/**
* Returns SQL compliant String representation of the day (YYYY-MM-DD)
*
* @return a string representation of the day
*
*/
public String toString()
{
return toSQLDate();
}//toString
/**
* Returns String representation of the day (DD.MM.YYYY)
* if normalDate is true, otherwise result is an SQL Date String
*
* @return a string representation of the day
*
*/
public String toString(boolean normalDate)
{
if (!normalDate)
return toSQLDate();
else
return day+"."+month+"."+year;
}//toString
/**
* Returns an SQL comliant DATE String
*
* @return an SQL comliant DATE String
*
*/
public String toSQLDate() {
String retval = "";
if (Math.abs(year) < 1000)
retval +="0";
if (Math.abs(year) < 100)
retval +="0";
if (Math.abs(year) < 10)
retval+="0";
retval +=year +"-";
if (month < 10)
retval+="0";
retval+= month +"-";
if (day < 10)
retval+="0";
retval+= day;
return retval;
}//toSQLDate
/**
* Compares this Day against another object
*
* @param obj another object
* @return true if the other object is identical to this Day object
*
*/
public boolean equals(Object obj)
{
if (!getClass().equals(obj.getClass()))
return false;
Day b = (Day)obj;
return day == b.day && month == b.month && year == b.year;
}//equals
//advances the date by the submitted number of days
//(may also be negative value)
public void advanceDate(int days) {
fromJulian(toJulian()+days);
}//moveDays
/**
* Calculates the number of days between this day and the
* submitted day.
*
* @param b any date
* @return the number of days between this day and b
* ( > 0 if this day comes after b )
*/
public int daysBetween(Day b)
{
return toJulian() - b.toJulian();
}//daysBetween
/*
* Computes the number of days between two dates
*
* @return true if this is a valid date
*/
public boolean isValid()
{
Day t = new Day();
t.fromJulian(this.toJulian());
return (t.day == day && t.month == month
&& t.year == year);
}//isValid
/*
* Converts a day to the julian calendar.
*
* @return The Julian day number that begins at noon of this day.
* Positive year signifies A.D., negative year B.C.
* Remember that the year after 1 B.C. was 1 A.D.
*
* A convenient reference point is that May 23, 1968 noon
* is Julian day 2440000.
*
* Julian day 0 is a Monday.
*
* This algorithm is from Press et al., Numerical Recipes
* in C, 2nd ed., Cambridge University Press 1992
*/
public int toJulian()
{
int jy = year;
if (year < 0) jy++;
int jm = month;
if (month > 2)
jm++;
else
{
jy--;
jm += 13;
}
int jul = (int) (java.lang.Math.floor(365.25 * jy)
+ java.lang.Math.floor(30.6001*jm) + day + 1720995.0);
// Gregorian Calendar adopted Oct. 15, 1582
int IGREG = 15 + 31*(10+12*1582);
// change over to Gregorian calendar
if (day + 31 * (month + 12 * year) >= IGREG)
{
int ja = (int)(0.01 * jy);
jul += 2 - ja + (int)(0.25 * ja);
}
return jul;
}//toJulian
/**
* Converts a Julian day to a calendar date
*
* This algorithm is from Press et al., Numerical Recipes
* in C, 2nd ed., Cambridge University Press 1992
*
* @param j the Julian date
*/
public void fromJulian(int j)
{
int ja = j;
//the Julian date of the adoption of the Gregorian calendar
int JGREG = 2299161;
//cross-over to Gregorian Calendar produces this correction
if (j >= JGREG)
{
int jalpha = (int)(((float)(j - 1867216) - 0.25) / 36524.25);
ja += 1 + jalpha - (int)(0.25 * jalpha);
}
int jb = ja + 1524;
int jc = (int)(6680.0 + ((float)(jb-2439870) - 122.1)/365.25);
int jd = (int)(365 * jc + (0.25 * jc));
int je = (int)((jb - jd)/30.6001);
day = jb - jd - (int)(30.6001 * je);
month = je - 1;
if (month > 12) month -= 12;
year = jc - 4715;
if (month > 2) --year;
if (year <= 0) --year;
}//fromJulan
}//Day
Du kanns einfacht mit Day myDay = new Day ein neues Objekt dieses Typs erzeugen, welches das aktuelle Datum enthält. Außerdem kanns Du auch mit Day(TT.MM.YYYY) oder Day(SQLDATUM) neue Day Objekte erzeugen. Die Klasse ist insgesamt sehr gut dokumentiert, und sollte eigentlich so verständlich sein. Mir hat sie vorallem im Zusammenspiel mit einer DB geholfen, weil man ganz einfach Daten ins SQL-Format übertragen kann, und einem insgesamt viel Arbeit abgenommen wird.
Grüße fs111
Lesezeichen