{@code Date} represents a specific moment in time, to the millisecond.
| Constructor: |
public Date() {
this(System.currentTimeMillis());
}
Initializes this {@code Date} instance to the current date and time. |
public Date(long milliseconds) {
this.milliseconds = milliseconds;
}
Initializes this {@code Date} instance using the specified millisecond value. The
value is the number of milliseconds since Jan. 1, 1970 GMT. Parameters:
milliseconds -
the number of milliseconds since Jan. 1, 1970 GMT.
|
public Date(String string) {
milliseconds = parse(string);
}
Constructs a new {@code Date} initialized to the date and time parsed from the
specified String. Parameters:
string -
the String to parse.
|
public Date(int year,
int month,
int day) {
GregorianCalendar cal = new GregorianCalendar(false);
cal.set(1900 + year, month, day);
milliseconds = cal.getTimeInMillis();
}
Constructs a new {@code Date} initialized to midnight in the default {@code TimeZone} on
the specified date. Parameters:
year -
the year, 0 is 1900.
month -
the month, 0 - 11.
day -
the day of the month, 1 - 31.
|
public Date(int year,
int month,
int day,
int hour,
int minute) {
GregorianCalendar cal = new GregorianCalendar(false);
cal.set(1900 + year, month, day, hour, minute);
milliseconds = cal.getTimeInMillis();
}
Constructs a new {@code Date} initialized to the specified date and time in the
default {@code TimeZone}. Parameters:
year -
the year, 0 is 1900.
month -
the month, 0 - 11.
day -
the day of the month, 1 - 31.
hour -
the hour of day, 0 - 23.
minute -
the minute of the hour, 0 - 59.
|
public Date(int year,
int month,
int day,
int hour,
int minute,
int second) {
GregorianCalendar cal = new GregorianCalendar(false);
cal.set(1900 + year, month, day, hour, minute, second);
milliseconds = cal.getTimeInMillis();
}
Constructs a new {@code Date} initialized to the specified date and time in the
default {@code TimeZone}. Parameters:
year -
the year, 0 is 1900.
month -
the month, 0 - 11.
day -
the day of the month, 1 - 31.
hour -
the hour of day, 0 - 23.
minute -
the minute of the hour, 0 - 59.
second -
the second of the minute, 0 - 59.
|
| Method from java.util.Date Detail: |
public static long UTC(int year,
int month,
int day,
int hour,
int minute,
int second) {
GregorianCalendar cal = new GregorianCalendar(false);
cal.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
cal.set(1900 + year, month, day, hour, minute, second);
return cal.getTimeInMillis();
} Deprecated! use - :
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal.set(year + 1900, month, day, hour, minute, second);
cal.getTime().getTime();
Returns the millisecond value of the specified date and time in GMT. |
public boolean after(Date date) {
return milliseconds > date.milliseconds;
}
Returns if this {@code Date} is after the specified Date. |
public boolean before(Date date) {
return milliseconds < date.milliseconds;
}
Returns if this {@code Date} is before the specified Date. |
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
Returns a new {@code Date} with the same millisecond value as this {@code Date}. |
public int compareTo(Date date) {
if (milliseconds < date.milliseconds) {
return -1;
}
if (milliseconds == date.milliseconds) {
return 0;
}
return 1;
}
Compare the receiver to the specified {@code Date} to determine the relative
ordering. |
public boolean equals(Object object) {
return (object == this) || (object instanceof Date)
&& (milliseconds == ((Date) object).milliseconds);
}
Compares the specified object to this {@code Date} and returns if they are equal.
To be equal, the object must be an instance of {@code Date} and have the same millisecond
value. |
public int getDate() {
return new GregorianCalendar(milliseconds).get(Calendar.DATE);
} Deprecated! use - {@code Calendar.get(Calendar.DATE)}
Returns the gregorian calendar day of the month for this {@code Date} object. |
public int getDay() {
return new GregorianCalendar(milliseconds).get(Calendar.DAY_OF_WEEK) - 1;
} Deprecated! use - {@code Calendar.get(Calendar.DAY_OF_WEEK)}
Returns the gregorian calendar day of the week for this {@code Date} object. |
public int getHours() {
return new GregorianCalendar(milliseconds).get(Calendar.HOUR_OF_DAY);
} Deprecated! use - {@code Calendar.get(Calendar.HOUR_OF_DAY)}
Returns the gregorian calendar hour of the day for this {@code Date} object. |
public int getMinutes() {
return new GregorianCalendar(milliseconds).get(Calendar.MINUTE);
} Deprecated! use - {@code Calendar.get(Calendar.MINUTE)}
Returns the gregorian calendar minute of the hour for this {@code Date} object. |
public int getMonth() {
return new GregorianCalendar(milliseconds).get(Calendar.MONTH);
} Deprecated! use - {@code Calendar.get(Calendar.MONTH)}
Returns the gregorian calendar month for this {@code Date} object. |
public int getSeconds() {
return new GregorianCalendar(milliseconds).get(Calendar.SECOND);
} Deprecated! use - {@code Calendar.get(Calendar.SECOND)}
Returns the gregorian calendar second of the minute for this {@code Date} object. |
public long getTime() {
return milliseconds;
}
Returns this {@code Date} as a millisecond value. The value is the number of
milliseconds since Jan. 1, 1970, midnight GMT. |
public int getTimezoneOffset() {
GregorianCalendar cal = new GregorianCalendar(milliseconds);
return -(cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 60000;
} Deprecated! use -
{@code (Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / 60000}
Returns the timezone offset in minutes of the default {@code TimeZone}. |
public int getYear() {
return new GregorianCalendar(milliseconds).get(Calendar.YEAR) - 1900;
} Deprecated! use - {@code Calendar.get(Calendar.YEAR) - 1900}
Returns the gregorian calendar year since 1900 for this {@code Date} object. |
public int hashCode() {
return (int) (milliseconds > > > 32) ^ (int) milliseconds;
}
Returns an integer hash code for the receiver. Objects which are equal
return the same value for this method. |
public static long parse(String string) {
if (string == null) {
// luni.06=The string argument is null
throw new IllegalArgumentException(Messages.getString("luni.06")); //$NON-NLS-1$
}
char sign = 0;
int commentLevel = 0;
int offset = 0, length = string.length(), state = 0;
int year = -1, month = -1, date = -1;
int hour = -1, minute = -1, second = -1, zoneOffset = 0, minutesOffset = 0;
boolean zone = false;
final int PAD = 0, LETTERS = 1, NUMBERS = 2;
StringBuilder buffer = new StringBuilder();
while (offset < = length) {
char next = offset < length ? string.charAt(offset) : '\r';
offset++;
if (next == '(') {
commentLevel++;
}
if (commentLevel > 0) {
if (next == ')') {
commentLevel--;
}
if (commentLevel == 0) {
next = ' ';
} else {
continue;
}
}
int nextState = PAD;
if ('a' < = next && next < = 'z' || 'A' < = next && next < = 'Z') {
nextState = LETTERS;
} else if ('0' < = next && next < = '9') {
nextState = NUMBERS;
} else if (!Character.isSpace(next) && ",+-:/".indexOf(next) == -1) { //$NON-NLS-1$
throw new IllegalArgumentException();
}
if (state == NUMBERS && nextState != NUMBERS) {
int digit = Integer.parseInt(buffer.toString());
buffer.setLength(0);
if (sign == '+' || sign == '-') {
if (zoneOffset == 0) {
zone = true;
if (next == ':') {
minutesOffset = sign == '-' ? -Integer
.parseInt(string.substring(offset,
offset + 2)) : Integer
.parseInt(string.substring(offset,
offset + 2));
offset += 2;
}
zoneOffset = sign == '-' ? -digit : digit;
sign = 0;
} else {
throw new IllegalArgumentException();
}
} else if (digit >= 70) {
if (year == -1
&& (Character.isSpace(next) || next == ','
|| next == '/' || next == '\r')) {
year = digit;
} else {
throw new IllegalArgumentException();
}
} else if (next == ':') {
if (hour == -1) {
hour = digit;
} else if (minute == -1) {
minute = digit;
} else {
throw new IllegalArgumentException();
}
} else if (next == '/') {
if (month == -1) {
month = digit - 1;
} else if (date == -1) {
date = digit;
} else {
throw new IllegalArgumentException();
}
} else if (Character.isSpace(next) || next == ','
|| next == '-' || next == '\r') {
if (hour != -1 && minute == -1) {
minute = digit;
} else if (minute != -1 && second == -1) {
second = digit;
} else if (date == -1) {
date = digit;
} else if (year == -1) {
year = digit;
} else {
throw new IllegalArgumentException();
}
} else if (year == -1 && month != -1 && date != -1) {
year = digit;
} else {
throw new IllegalArgumentException();
}
} else if (state == LETTERS && nextState != LETTERS) {
String text = buffer.toString().toUpperCase();
buffer.setLength(0);
if (text.length() == 1) {
throw new IllegalArgumentException();
}
if (text.equals("AM")) { //$NON-NLS-1$
if (hour == 12) {
hour = 0;
} else if (hour < 1 || hour > 12) {
throw new IllegalArgumentException();
}
} else if (text.equals("PM")) { //$NON-NLS-1$
if (hour == 12) {
hour = 0;
} else if (hour < 1 || hour > 12) {
throw new IllegalArgumentException();
}
hour += 12;
} else {
DateFormatSymbols symbols = new DateFormatSymbols(Locale.US);
String[] weekdays = symbols.getWeekdays(), months = symbols
.getMonths();
int value;
if (parse(text, weekdays) != -1) {/* empty */
} else if (month == -1
&& (month = parse(text, months)) != -1) {/* empty */
} else if (text.equals("GMT") || text.equals("UT") //$NON-NLS-1$ //$NON-NLS-2$
|| text.equals("UTC")) { //$NON-NLS-1$
zone = true;
zoneOffset = 0;
} else if ((value = zone(text)) != 0) {
zone = true;
zoneOffset = value;
} else {
throw new IllegalArgumentException();
}
}
}
if (next == '+' || (year != -1 && next == '-')) {
sign = next;
} else if (!Character.isSpace(next) && next != ','
&& nextState != NUMBERS) {
sign = 0;
}
if (nextState == LETTERS || nextState == NUMBERS) {
buffer.append(next);
}
state = nextState;
}
if (year != -1 && month != -1 && date != -1) {
if (hour == -1) {
hour = 0;
}
if (minute == -1) {
minute = 0;
}
if (second == -1) {
second = 0;
}
if (year < (creationYear - 80)) {
year += 2000;
} else if (year < 100) {
year += 1900;
}
minute -= minutesOffset;
if (zone) {
if (zoneOffset >= 24 || zoneOffset < = -24) {
hour -= zoneOffset / 100;
minute -= zoneOffset % 100;
} else {
hour -= zoneOffset;
}
return UTC(year - 1900, month, date, hour, minute, second);
}
return new Date(year - 1900, month, date, hour, minute, second)
.getTime();
}
throw new IllegalArgumentException();
} Deprecated! use - DateFormat
Returns the millisecond value of the date and time parsed from the
specified {@code String}. Many date/time formats are recognized, including IETF
standard syntax, i.e. Tue, 22 Jun 1999 12:16:00 GMT-0500 |
public void setDate(int day) {
GregorianCalendar cal = new GregorianCalendar(milliseconds);
cal.set(Calendar.DATE, day);
milliseconds = cal.getTimeInMillis();
} Deprecated! use - {@code Calendar.set(Calendar.DATE, day)}
Sets the gregorian calendar day of the month for this {@code Date} object. |
public void setHours(int hour) {
GregorianCalendar cal = new GregorianCalendar(milliseconds);
cal.set(Calendar.HOUR_OF_DAY, hour);
milliseconds = cal.getTimeInMillis();
} Deprecated! use - {@code Calendar.set(Calendar.HOUR_OF_DAY, hour)}
Sets the gregorian calendar hour of the day for this {@code Date} object. |
public void setMinutes(int minute) {
GregorianCalendar cal = new GregorianCalendar(milliseconds);
cal.set(Calendar.MINUTE, minute);
milliseconds = cal.getTimeInMillis();
} Deprecated! use - {@code Calendar.set(Calendar.MINUTE, minute)}
Sets the gregorian calendar minute of the hour for this {@code Date} object. |
public void setMonth(int month) {
GregorianCalendar cal = new GregorianCalendar(milliseconds);
cal.set(Calendar.MONTH, month);
milliseconds = cal.getTimeInMillis();
} Deprecated! use - {@code Calendar.set(Calendar.MONTH, month)}
Sets the gregorian calendar month for this {@code Date} object. |
public void setSeconds(int second) {
GregorianCalendar cal = new GregorianCalendar(milliseconds);
cal.set(Calendar.SECOND, second);
milliseconds = cal.getTimeInMillis();
} Deprecated! use - {@code Calendar.set(Calendar.SECOND, second)}
Sets the gregorian calendar second of the minute for this {@code Date} object. |
public void setTime(long milliseconds) {
this.milliseconds = milliseconds;
}
Sets this {@code Date} to the specified millisecond value. The value is the
number of milliseconds since Jan. 1, 1970 GMT. |
public void setYear(int year) {
GregorianCalendar cal = new GregorianCalendar(milliseconds);
cal.set(Calendar.YEAR, year + 1900);
milliseconds = cal.getTimeInMillis();
} Deprecated! use - {@code Calendar.set(Calendar.YEAR, year + 1900)}
Sets the gregorian calendar year since 1900 for this {@code Date} object. |
public String toGMTString() {
SimpleDateFormat format1 = new SimpleDateFormat("d MMM ", Locale.US); //$NON-NLS-1$
SimpleDateFormat format2 = new SimpleDateFormat(
" HH:mm:ss 'GMT'", Locale.US); //$NON-NLS-1$
TimeZone gmtZone = TimeZone.getTimeZone("GMT"); //$NON-NLS-1$
format1.setTimeZone(gmtZone);
format2.setTimeZone(gmtZone);
GregorianCalendar gc = new GregorianCalendar(gmtZone);
gc.setTimeInMillis(milliseconds);
return format1.format(this) + gc.get(Calendar.YEAR)
+ format2.format(this);
} Deprecated! use - DateFormat
Returns the string representation of this {@code Date} in GMT in the format: 22
Jun 1999 13:02:00 GMT |
public String toLocaleString() {
return DateFormat.getDateTimeInstance().format(this);
} Deprecated! use - DateFormat
Returns the string representation of this {@code Date} for the default {@code Locale}. |
public String toString() {
Calendar cal = new GregorianCalendar(milliseconds);
return dayOfWeekNames[cal.get(Calendar.DAY_OF_WEEK) - 1] + " " + monthNames[cal.get(Calendar.MONTH)]//$NON-NLS-1$
+ " " + toTwoDigits(cal.get(Calendar.DAY_OF_MONTH)) + " " + toTwoDigits(cal.get(Calendar.HOUR_OF_DAY))//$NON-NLS-1$ //$NON-NLS-2$
+ ":" + toTwoDigits(cal.get(Calendar.MINUTE)) + ":" + toTwoDigits(cal.get(Calendar.SECOND))//$NON-NLS-1$ //$NON-NLS-2$
+ " " + cal.getTimeZone().getID() + " " + cal.get(Calendar.YEAR);//$NON-NLS-1$ //$NON-NLS-2$
}
Returns the string representation of this {@code Date} in the format: Tue Jun 22
13:07:00 GMT 1999 |