// Consider using GMTZone here...
import java.util.*;
import java.text.*;
/**
* NewDate gives back the function of the old java.util.Date
* class, and more. All the public methods (including the deprecated
* ones) are here. Still it is not a duplicate. It respects
* locale and other internationalization issues. And as a subclass
* of GregorianCalendar, you can use the date-math functions add() and
* roll() without problems and without having to learn all the variables
* (WEEK_OF_YEAR, etc.) in java.util.Calendar, unless you really need to.
*
* Some methods are synchronized (slow) but they have to be if several
* threads will be accessing the same instance and setting values or
* using date math functions from Calendar. In some cases the format
* of output strings will not match what java.util.Date put out, but I've
* made an effort to format the output strings exactly as Date did. You
* can get whatever format you want by passing the result of NewDate's
* getTime() method (which returns a real java.util.Date) to SimpleDateFormat's
* format() method.
*
* Note that one thing you cannot do is cast a NewDate
* to a Date. So instead of:
* DateFormat.getDateInstance().format( newDate ); // won't compile
*
* you must write:
* Dateformat.getDateInstance().format( newDate.getTime() ); // okay
*
*
* Use this *freeware* code at your own risk, of course. But please let
* me know if you find bugs or need additional function. My TimeZone
* fix for the GMT meridian, GMTZone
* can be used with this class if you edit this code replacing
* "TimeZone.getTimeZone(" with "GMTZone.getTimeZone(", and recompile it.
* Tested with 40 concurrent threads accessing the same instance of NewDate.
* Compiles to 3,438 bytes.
* @author Tony Dahlman
* @version 1.0 1999.05.24
*/
public class NewDate extends GregorianCalendar {
/**
* A formatter for methods like toString() and toLocaleString(),
* which now behave identically by the way. This will not be instantiated
* unless you call the constructor with a String argument, or one of
* the methods that return a formatted date String.
*/
protected DateFormat df = null;
/**
* A formatter for the toGMTString() function only. It is not
* instantiated unless that method is used.
*/
protected SimpleDateFormat gmtf = null;
/** Default constructor. */
public NewDate() {
super();
}
/** Construct a NewDate from a UTC time in milliseconds since 1970. */
public NewDate( long millis ) {
super();
this.setTime( new Date(millis) );
}
/** Construct a NewDate from year, month and day. */
public NewDate( int yr, int mo, int da ) {
super( yr, mo, da );
}
/** Construct a NewDate from date and time values */
public NewDate( int yr, int mo, int da, int hr, int min ) {
super( yr, mo, da, hr, min );
}
/** Construct a NewDate from date and time values */
public NewDate( int yr, int mo, int da, int hr, int min, int sec ) {
super( yr, mo, da, hr, min, sec );
}
/**
* Construct a NewDate from a date string such as
* "Sat, 12 Aug 1995 13:30:00 GMT+0430". Uses NumberFormat's parse()
* method, so the string should be more or less what people write
* in the current language and country (Locale).
*/
public NewDate( String s ) throws ParseException {
super();
df = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.LONG);
df.setTimeZone( TimeZone.getDefault() );
this.setTime( df.parse(s) );
}
/**
* Class method to get a long from UTC date values. Year is the
* number of years since 1900. Months are numbered 0 to 11, as per
* usual for Java.
*/
public static long UTC( int yr, int mo, int da, int hr, int min, int sec ) {
GregorianCalendar cal = new GregorianCalendar(
1900 + yr, mo, da, hr, min, sec );
return cal.getTime().getTime();
}
/**
* Class method to get a long by parsing a date string such as
* "Sat, 12 Aug 1995 13:30:00 GMT+0430".
*/
public static long parse( String s ) throws ParseException {
return DateFormat.getDateTimeInstance(
DateFormat.FULL, DateFormat.LONG).parse(s).getTime();
}
/** Gets the year. */
public int getYear() {
return this.get( Calendar.YEAR );
}
/** Sets the year. */
public synchronized void setYear( int yr ) {
this.set( Calendar.YEAR, yr );
}
/** Gets the month, using month numbers 0-11, as per usual for Java. */
public int getMonth() {
return this.get( Calendar.MONTH );
}
/** Sets the month, using month numbers 0-11, as per usual for Java. */
public synchronized void setMonth( int mo ) {
this.set( Calendar.MONTH, mo );
}
/** Gets the day of the month, 1-31. */
public int getDate() {
return this.get( Calendar.DATE );
}
/** Sets the day of the month, 1 thru 28, 29, 30 or 31. */
public synchronized void setDate( int da ) {
this.set( Calendar.DATE, da );
}
/** Gets the day of the week in range 0 (Sunday) to 6 (Saturday). */
public int getDay() {
return this.get( Calendar.DAY_OF_WEEK ) - 1;
}
/** Gets the hours part of the current time, 0-23. */
public int getHours() {
return this.get( Calendar.HOUR_OF_DAY );
}
/** Sets the hours part of the current time, 0-23. */
public synchronized void setHours( int hr ) {
this.set( Calendar.HOUR_OF_DAY, hr );
}
/** Gets the minutes part of the current time. */
public int getMinutes() {
return this.get( Calendar.MINUTE );
}
/** Sets the minutes part of the current time. */
public synchronized void setMinutes( int min ) {
this.set( Calendar.MINUTE, min );
}
/** Gets the seconds part of the current time. */
public int getSeconds() {
return this.get( Calendar.SECOND );
}
/** Sets the seconds part of the current time. */
public synchronized void setSeconds( int sec ) {
this.set( Calendar.SECOND, sec );
}
/**
* Returns boolean "true" if the date/time of this object
* is before some other date.
*/
public boolean before( Date when ) {
return this.getTime().before( when );
}
/**
* Returns boolean "true" if the date/time of this object
* is after some other date.
*/
public boolean after( Date when ) {
return this.getTime().after( when );
}
/**
* Returns boolean "true" if the date/time of this
* object (which must be a Date) is the same (to the millisecond) as some
* other Date object.
*/
public boolean equals( Object when ) {
if( when instanceof Calendar )
return this.equals( when );
else if( when instanceof Date )
return this.getTime().equals( when );
return false;
}
/**
* Returns string representation of this object's date/time.
* Uses DateFormat, the current Locale and TimeZone to do this.
* As such, there is no difference between this method and
* NewDate's toLocaleString() method.
*/
public String toString() {
if( df == null ) {
DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.LONG);
df.setTimeZone( TimeZone.getDefault() );
}
return df.format( this.getTime() );
}
/** Set this object's time from a long integer. */
public synchronized void setTime( long millis ) {
this.setTime( new Date(millis) );
}
/**
* Returns string representation of this object's date/time.
* Uses DateFormat, the current Locale and TimeZone to do this.
* As such, there is no difference between this method and
* NewDates toString() method.
*/
public String toLocaleString() {
return this.toString();
}
/**
* Duplicates the deprecated function in java.util.Date by
* returning a string representation of this object's date/time.
* Uses DateFormat, the current Locale and the UTC TimeZone ID to
* do so.
*/
public String toGMTString() {
if( gmtf == null ) {
gmtf = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
gmtf.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
}
return gmtf.format( this.getTime() ) + " GMT";
}
/**
* Sets the time using the int provided by Unix, which I
* understand to be seconds since 1970.
*/
public void setDateFromUnix( int unixDate ) {
// 64-bit seconds since 1970, *1000 gives millis
long millis = (unixDate & 0x00000000FFFFFFFFL) * 1000L;
setTime( millis );
}
/**
* Returns a long (there is no Java unsigned int) representing
* this date's number of seconds since 1970.
*/
public long getDateAsUnix() {
// we have to return a long since unsigned int doesn't exist
return getTime().getTime() / 1000L;
}
/**
* Behaves like the deprecated java.util.Date method.
* "Returns the local time-zone offset. The time-zone offset is the
* number of minutes that must be added to GMT to give the local time
* zone. This value includes the correction, if necessary, for daylight
* saving time."
*/
public int getTimezoneOffset() {
return - ( internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET) ) / ( 1000 * 60 );
}
}