Class NewDate

java.lang.Object
   |
   +----java.util.Calendar
           |
           +----java.util.GregorianCalendar
                   |
                   +----NewDate

public class NewDate
extends GregorianCalendar
NewDate gives back all of 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 pays due respect to 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 to SimpleDateFormat's format() method.

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,536 bytes.

Version:
1.0 1999.05.24
Author:
Tony Dahlman

Variable Index

 o df
A formatter for methods like toString() and toLocaleString(), which now behave identically by the way.
 o gmtf
A formatter for the toGMTString() function only.
 o isDftzSet
A flag to track whether timezone has been set for the default formatter
 o isGmtfSet
A flag to track whether the GMT formatter has been instantiated.

Constructor Index

 o NewDate()
Default constructor.
 o NewDate(int, int, int)
Construct a NewDate from year, month and day.
 o NewDate(int, int, int, int, int)
Construct a NewDate from date and time values
 o NewDate(int, int, int, int, int, int)
Construct a NewDate from date and time values
 o NewDate(long)
Construct a NewDate from a UTC time in milliseconds since 1970.
 o NewDate(String)
Construct a NewDate from a date string such as "Sat, 12 Aug 1995 13:30:00 GMT+0430".

Method Index

 o after(Date)
Returns boolean "true" if the date/time of this object is after some other date.
 o before(Date)
Returns boolean "true" if the date/time of this object is before some other date.
 o equals(Object)
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.
 o getDate()
Gets the day of the month, 1-31.
 o getDay()
Gets the day of the week in range 0 (Sunday) to 6 (Saturday).
 o getHours()
Gets the hours part of the current time, 0-23.
 o getMinutes()
Gets the minutes part of the current time.
 o getMonth()
Gets the month, using month numbers 0-11, as per usual for Java.
 o getSeconds()
Gets the seconds part of the current time.
 o getYear()
Gets the year.
 o parse(String)
Class method to get a long by parsing a date string such as "Sat, 12 Aug 1995 13:30:00 GMT+0430".
 o setDate(int)
Sets the day of the month, 1 thru 28, 29, 30 or 31.
 o setHours(int)
Sets the hours part of the current time, 0-23.
 o setMinutes(int)
Sets the minutes part of the current time.
 o setMonth(int)
Sets the month, using month numbers 0-11, as per usual for Java.
 o setSeconds(int)
Sets the seconds part of the current time.
 o setTime(long)
Set this object's time from a long integer.
 o setYear(int)
Sets the year.
 o toGMTString()
Duplicates the deprecated function in java.util.Date by returning a string representation of this object's date/time.
 o toLocaleString()
Returns string representation of this object's date/time.
 o toString()
Returns string representation of this object's date/time.
 o UTC(int, int, int, int, int, int)
Class method to get a long from UTC date values.

Variables

 o df
 protected DateFormat df
A formatter for methods like toString() and toLocaleString(), which now behave identically by the way.

 o isDftzSet
 private boolean isDftzSet
A flag to track whether timezone has been set for the default formatter

 o gmtf
 protected SimpleDateFormat gmtf
A formatter for the toGMTString() function only. It is not instantiated unless that method is used.

 o isGmtfSet
 private boolean isGmtfSet
A flag to track whether the GMT formatter has been instantiated.

Constructors

 o NewDate
 public NewDate()
Default constructor.

 o NewDate
 public NewDate(long millis)
Construct a NewDate from a UTC time in milliseconds since 1970.

 o NewDate
 public NewDate(int yr,
                int mo,
                int da)
Construct a NewDate from year, month and day.

 o NewDate
 public NewDate(int yr,
                int mo,
                int da,
                int hr,
                int min)
Construct a NewDate from date and time values

 o NewDate
 public NewDate(int yr,
                int mo,
                int da,
                int hr,
                int min,
                int sec)
Construct a NewDate from date and time values

 o NewDate
 public NewDate(String s) throws ParseException
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).

Methods

 o UTC
 public static long UTC(int yr,
                        int mo,
                        int da,
                        int hr,
                        int min,
                        int sec)
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.

 o parse
 public static long parse(String s) throws ParseException
Class method to get a long by parsing a date string such as "Sat, 12 Aug 1995 13:30:00 GMT+0430".

 o getYear
 public int getYear()
Gets the year.

 o setYear
 public synchronized void setYear(int yr)
Sets the year.

 o getMonth
 public int getMonth()
Gets the month, using month numbers 0-11, as per usual for Java.

 o setMonth
 public synchronized void setMonth(int mo)
Sets the month, using month numbers 0-11, as per usual for Java.

 o getDate
 public int getDate()
Gets the day of the month, 1-31.

 o setDate
 public synchronized void setDate(int da)
Sets the day of the month, 1 thru 28, 29, 30 or 31.

 o getDay
 public int getDay()
Gets the day of the week in range 0 (Sunday) to 6 (Saturday).

 o getHours
 public int getHours()
Gets the hours part of the current time, 0-23.

 o setHours
 public synchronized void setHours(int hr)
Sets the hours part of the current time, 0-23.

 o getMinutes
 public int getMinutes()
Gets the minutes part of the current time.

 o setMinutes
 public synchronized void setMinutes(int min)
Sets the minutes part of the current time.

 o getSeconds
 public int getSeconds()
Gets the seconds part of the current time.

 o setSeconds
 public synchronized void setSeconds(int sec)
Sets the seconds part of the current time.

 o before
 public boolean before(Date when)
Returns boolean "true" if the date/time of this object is before some other date.

 o after
 public boolean after(Date when)
Returns boolean "true" if the date/time of this object is after some other date.

 o equals
 public boolean equals(Object 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.

Overrides:
equals in class GregorianCalendar
 o toString
 public String toString()
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 toLocaleString() method.

Overrides:
toString in class Calendar
 o setTime
 public synchronized void setTime(long millis)
Set this object's time from a long integer.

 o toLocaleString
 public String toLocaleString()
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.

 o toGMTString
 public String toGMTString()
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.