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
-
df
- A formatter for methods like toString() and toLocaleString(),
which now behave identically by the way.
-
gmtf
- A formatter for the toGMTString() function only.
-
isDftzSet
- A flag to track whether timezone has been set for the default formatter
-
isGmtfSet
- A flag to track whether the GMT formatter has been instantiated.
-
NewDate()
- Default constructor.
-
NewDate(int, int, int)
- Construct a NewDate from year, month and day.
-
NewDate(int, int, int, int, int)
- Construct a NewDate from date and time values
-
NewDate(int, int, int, int, int, int)
- Construct a NewDate from date and time values
-
NewDate(long)
- Construct a NewDate from a UTC time in milliseconds since 1970.
-
NewDate(String)
-
Construct a NewDate from a date string such as
"Sat, 12 Aug 1995 13:30:00 GMT+0430".
-
after(Date)
-
Returns boolean "true" if the date/time of this object
is after some other date.
-
before(Date)
-
Returns boolean "true" if the date/time of this object
is before some other date.
-
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.
-
getDate()
- Gets the day of the month, 1-31.
-
getDay()
- Gets the day of the week in range 0 (Sunday) to 6 (Saturday).
-
getHours()
- Gets the hours part of the current time, 0-23.
-
getMinutes()
- Gets the minutes part of the current time.
-
getMonth()
- Gets the month, using month numbers 0-11, as per usual for Java.
-
getSeconds()
- Gets the seconds part of the current time.
-
getYear()
- Gets the year.
-
parse(String)
- Class method to get a long by parsing a date string such as
"Sat, 12 Aug 1995 13:30:00 GMT+0430".
-
setDate(int)
- Sets the day of the month, 1 thru 28, 29, 30 or 31.
-
setHours(int)
- Sets the hours part of the current time, 0-23.
-
setMinutes(int)
- Sets the minutes part of the current time.
-
setMonth(int)
- Sets the month, using month numbers 0-11, as per usual for Java.
-
setSeconds(int)
- Sets the seconds part of the current time.
-
setTime(long)
- Set this object's time from a long integer.
-
setYear(int)
- Sets the year.
-
toGMTString()
- Duplicates the deprecated function in java.util.Date by
returning a string representation of this object's date/time.
-
toLocaleString()
-
Returns string representation of this object's date/time.
-
toString()
-
Returns string representation of this object's date/time.
-
UTC(int, int, int, int, int, int)
-
Class method to get a long from UTC date values.
df
protected DateFormat df
- A formatter for methods like toString() and toLocaleString(),
which now behave identically by the way.
isDftzSet
private boolean isDftzSet
- A flag to track whether timezone has been set for the default formatter
gmtf
protected SimpleDateFormat gmtf
- A formatter for the toGMTString() function only. It is not
instantiated unless that method is used.
isGmtfSet
private boolean isGmtfSet
- A flag to track whether the GMT formatter has been instantiated.
NewDate
public NewDate()
- Default constructor.
NewDate
public NewDate(long millis)
- Construct a NewDate from a UTC time in milliseconds since 1970.
NewDate
public NewDate(int yr,
int mo,
int da)
- Construct a NewDate from year, month and day.
NewDate
public NewDate(int yr,
int mo,
int da,
int hr,
int min)
- Construct a NewDate from date and time values
NewDate
public NewDate(int yr,
int mo,
int da,
int hr,
int min,
int sec)
- Construct a NewDate from date and time values
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).
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.
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".
getYear
public int getYear()
- Gets the year.
setYear
public synchronized void setYear(int yr)
- Sets the year.
getMonth
public int getMonth()
- Gets the month, using month numbers 0-11, as per usual for Java.
setMonth
public synchronized void setMonth(int mo)
- Sets the month, using month numbers 0-11, as per usual for Java.
getDate
public int getDate()
- Gets the day of the month, 1-31.
setDate
public synchronized void setDate(int da)
- Sets the day of the month, 1 thru 28, 29, 30 or 31.
getDay
public int getDay()
- Gets the day of the week in range 0 (Sunday) to 6 (Saturday).
getHours
public int getHours()
- Gets the hours part of the current time, 0-23.
setHours
public synchronized void setHours(int hr)
- Sets the hours part of the current time, 0-23.
getMinutes
public int getMinutes()
- Gets the minutes part of the current time.
setMinutes
public synchronized void setMinutes(int min)
- Sets the minutes part of the current time.
getSeconds
public int getSeconds()
- Gets the seconds part of the current time.
setSeconds
public synchronized void setSeconds(int sec)
- Sets the seconds part of the current time.
before
public boolean before(Date when)
- Returns boolean "true" if the date/time of this object
is before some other date.
after
public boolean after(Date when)
- Returns boolean "true" if the date/time of this object
is after some other date.
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
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
setTime
public synchronized void setTime(long millis)
- Set this object's time from a long integer.
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.
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.