Fixes some compilation errors and applies formatting

This commit is contained in:
konsoletyper 2014-05-15 18:25:45 +04:00
parent 2817f7603d
commit 0cd1ddedc5
5 changed files with 160 additions and 2074 deletions

View File

@ -47,34 +47,16 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
private transient long milliseconds; private transient long milliseconds;
@SuppressWarnings("nls") @SuppressWarnings("nls")
private static String[] dayOfWeekNames = { "Sun", "Mon", "Tue", "Wed", private static String[] dayOfWeekNames = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
"Thu", "Fri", "Sat" };
@SuppressWarnings("nls") @SuppressWarnings("nls")
private static String[] monthNames = { "Jan", "Feb", "Mar", "Apr", "May", private static String[] monthNames = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; "Dec" };
/**
* Initializes this {@code Date} instance to the current date and time.
*/
public Date() { public Date() {
this(System.currentTimeMillis()); this(System.currentTimeMillis());
} }
/**
* Constructs a new {@code Date} initialized to midnight in the default {@code TimeZone} on
* the specified date.
*
* @param year
* the year, 0 is 1900.
* @param month
* the month, 0 - 11.
* @param day
* the day of the month, 1 - 31.
*
* @deprecated use
* {@link GregorianCalendar#GregorianCalendar(int, int, int)}
*/
@Deprecated @Deprecated
public Date(int year, int month, int day) { public Date(int year, int month, int day) {
GregorianCalendar cal = new GregorianCalendar(false); GregorianCalendar cal = new GregorianCalendar(false);
@ -82,24 +64,6 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Constructs a new {@code Date} initialized to the specified date and time in the
* default {@code TimeZone}.
*
* @param year
* the year, 0 is 1900.
* @param month
* the month, 0 - 11.
* @param day
* the day of the month, 1 - 31.
* @param hour
* the hour of day, 0 - 23.
* @param minute
* the minute of the hour, 0 - 59.
*
* @deprecated use
* {@link GregorianCalendar#GregorianCalendar(int, int, int, int, int)}
*/
@Deprecated @Deprecated
public Date(int year, int month, int day, int hour, int minute) { public Date(int year, int month, int day, int hour, int minute) {
GregorianCalendar cal = new GregorianCalendar(false); GregorianCalendar cal = new GregorianCalendar(false);
@ -107,26 +71,6 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Constructs a new {@code Date} initialized to the specified date and time in the
* default {@code TimeZone}.
*
* @param year
* the year, 0 is 1900.
* @param month
* the month, 0 - 11.
* @param day
* the day of the month, 1 - 31.
* @param hour
* the hour of day, 0 - 23.
* @param minute
* the minute of the hour, 0 - 59.
* @param second
* the second of the minute, 0 - 59.
*
* @deprecated use
* {@link GregorianCalendar#GregorianCalendar(int, int, int, int, int, int)}
*/
@Deprecated @Deprecated
public Date(int year, int month, int day, int hour, int minute, int second) { public Date(int year, int month, int day, int hour, int minute, int second) {
GregorianCalendar cal = new GregorianCalendar(false); GregorianCalendar cal = new GregorianCalendar(false);
@ -134,62 +78,23 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Initializes this {@code Date} instance using the specified millisecond value. The
* value is the number of milliseconds since Jan. 1, 1970 GMT.
*
* @param milliseconds
* the number of milliseconds since Jan. 1, 1970 GMT.
*/
public Date(long milliseconds) { public Date(long milliseconds) {
this.milliseconds = milliseconds; this.milliseconds = milliseconds;
} }
/**
* Constructs a new {@code Date} initialized to the date and time parsed from the
* specified String.
*
* @param string
* the String to parse.
*
* @deprecated use {@link DateFormat}
*/
@Deprecated @Deprecated
public Date(String string) { public Date(String string) {
milliseconds = parse(string); milliseconds = parse(string);
} }
/**
* Returns if this {@code Date} is after the specified Date.
*
* @param date
* a Date instance to compare.
* @return {@code true} if this {@code Date} is after the specified {@code Date},
* {@code false} otherwise.
*/
public boolean after(Date date) { public boolean after(Date date) {
return milliseconds > date.milliseconds; return milliseconds > date.milliseconds;
} }
/**
* Returns if this {@code Date} is before the specified Date.
*
* @param date
* a {@code Date} instance to compare.
* @return {@code true} if this {@code Date} is before the specified {@code Date},
* {@code false} otherwise.
*/
public boolean before(Date date) { public boolean before(Date date) {
return milliseconds < date.milliseconds; return milliseconds < date.milliseconds;
} }
/**
* Returns a new {@code Date} with the same millisecond value as this {@code Date}.
*
* @return a shallow copy of this {@code Date}.
*
* @see java.lang.Cloneable
*/
@Override @Override
public Object clone() { public Object clone() {
try { try {
@ -199,15 +104,6 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
} }
} }
/**
* Compare the receiver to the specified {@code Date} to determine the relative
* ordering.
*
* @param date
* a {@code Date} to compare against.
* @return an {@code int < 0} if this {@code Date} is less than the specified {@code Date}, {@code 0} if
* they are equal, and an {@code int > 0} if this {@code Date} is greater.
*/
public int compareTo(Date date) { public int compareTo(Date date) {
if (milliseconds < date.milliseconds) { if (milliseconds < date.milliseconds) {
return -1; return -1;
@ -218,140 +114,56 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
return 1; return 1;
} }
/**
* 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.
*
* @param object
* the object to compare with this object.
* @return {@code true} if the specified object is equal to this {@code Date}, {@code false}
* otherwise.
*
* @see #hashCode
*/
@Override @Override
public boolean equals(Object object) { public boolean equals(Object object) {
return (object == this) || (object instanceof Date) return (object == this) || (object instanceof Date) && (milliseconds == ((Date) object).milliseconds);
&& (milliseconds == ((Date) object).milliseconds);
} }
/**
* Returns the gregorian calendar day of the month for this {@code Date} object.
*
* @return the day of the month.
*
* @deprecated use {@code Calendar.get(Calendar.DATE)}
*/
@Deprecated @Deprecated
public int getDate() { public int getDate() {
return new GregorianCalendar(milliseconds).get(Calendar.DATE); return new GregorianCalendar(milliseconds).get(Calendar.DATE);
} }
/**
* Returns the gregorian calendar day of the week for this {@code Date} object.
*
* @return the day of the week.
*
* @deprecated use {@code Calendar.get(Calendar.DAY_OF_WEEK)}
*/
@Deprecated @Deprecated
public int getDay() { public int getDay() {
return new GregorianCalendar(milliseconds).get(Calendar.DAY_OF_WEEK) - 1; return new GregorianCalendar(milliseconds).get(Calendar.DAY_OF_WEEK) - 1;
} }
/**
* Returns the gregorian calendar hour of the day for this {@code Date} object.
*
* @return the hour of the day.
*
* @deprecated use {@code Calendar.get(Calendar.HOUR_OF_DAY)}
*/
@Deprecated @Deprecated
public int getHours() { public int getHours() {
return new GregorianCalendar(milliseconds).get(Calendar.HOUR_OF_DAY); return new GregorianCalendar(milliseconds).get(Calendar.HOUR_OF_DAY);
} }
/**
* Returns the gregorian calendar minute of the hour for this {@code Date} object.
*
* @return the minutes.
*
* @deprecated use {@code Calendar.get(Calendar.MINUTE)}
*/
@Deprecated @Deprecated
public int getMinutes() { public int getMinutes() {
return new GregorianCalendar(milliseconds).get(Calendar.MINUTE); return new GregorianCalendar(milliseconds).get(Calendar.MINUTE);
} }
/**
* Returns the gregorian calendar month for this {@code Date} object.
*
* @return the month.
*
* @deprecated use {@code Calendar.get(Calendar.MONTH)}
*/
@Deprecated @Deprecated
public int getMonth() { public int getMonth() {
return new GregorianCalendar(milliseconds).get(Calendar.MONTH); return new GregorianCalendar(milliseconds).get(Calendar.MONTH);
} }
/**
* Returns the gregorian calendar second of the minute for this {@code Date} object.
*
* @return the seconds.
*
* @deprecated use {@code Calendar.get(Calendar.SECOND)}
*/
@Deprecated @Deprecated
public int getSeconds() { public int getSeconds() {
return new GregorianCalendar(milliseconds).get(Calendar.SECOND); return new GregorianCalendar(milliseconds).get(Calendar.SECOND);
} }
/**
* Returns this {@code Date} as a millisecond value. The value is the number of
* milliseconds since Jan. 1, 1970, midnight GMT.
*
* @return the number of milliseconds since Jan. 1, 1970, midnight GMT.
*/
public long getTime() { public long getTime() {
return milliseconds; return milliseconds;
} }
/**
* Returns the timezone offset in minutes of the default {@code TimeZone}.
*
* @return the timezone offset in minutes of the default {@code TimeZone}.
*
* @deprecated use
* {@code (Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / 60000}
*/
@Deprecated @Deprecated
public int getTimezoneOffset() { public int getTimezoneOffset() {
GregorianCalendar cal = new GregorianCalendar(milliseconds); GregorianCalendar cal = new GregorianCalendar(milliseconds);
return -(cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 60000; return -(cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 60000;
} }
/**
* Returns the gregorian calendar year since 1900 for this {@code Date} object.
*
* @return the year - 1900.
*
* @deprecated use {@code Calendar.get(Calendar.YEAR) - 1900}
*/
@Deprecated @Deprecated
public int getYear() { public int getYear() {
return new GregorianCalendar(milliseconds).get(Calendar.YEAR) - 1900; return new GregorianCalendar(milliseconds).get(Calendar.YEAR) - 1900;
} }
/**
* Returns an integer hash code for the receiver. Objects which are equal
* return the same value for this method.
*
* @return this {@code Date}'s hash.
*
* @see #equals
*/
@Override @Override
public int hashCode() { public int hashCode() {
return (int) (milliseconds >>> 32) ^ (int) milliseconds; return (int) (milliseconds >>> 32) ^ (int) milliseconds;
@ -366,23 +178,12 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
return -1; return -1;
} }
/**
* 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
*
* @param string
* the String to parse.
* @return the millisecond value parsed from the String.
*
* @deprecated use {@link DateFormat}
*/
@Deprecated @Deprecated
public static long parse(String string) { public static long parse(String string) {
if (string == null) { if (string == null) {
// luni.06=The string argument is null // luni.06=The string argument is null
throw new IllegalArgumentException(Messages.getString("luni.06")); //$NON-NLS-1$ throw new IllegalArgumentException();
} }
char sign = 0; char sign = 0;
@ -428,11 +229,8 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
if (zoneOffset == 0) { if (zoneOffset == 0) {
zone = true; zone = true;
if (next == ':') { if (next == ':') {
minutesOffset = sign == '-' ? -Integer minutesOffset = sign == '-' ? -Integer.parseInt(string.substring(offset, offset + 2))
.parseInt(string.substring(offset, : Integer.parseInt(string.substring(offset, offset + 2));
offset + 2)) : Integer
.parseInt(string.substring(offset,
offset + 2));
offset += 2; offset += 2;
} }
zoneOffset = sign == '-' ? -digit : digit; zoneOffset = sign == '-' ? -digit : digit;
@ -441,9 +239,7 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} else if (digit >= 70) { } else if (digit >= 70) {
if (year == -1 if (year == -1 && (Character.isSpace(next) || next == ',' || next == '/' || next == '\r')) {
&& (Character.isSpace(next) || next == ','
|| next == '/' || next == '\r')) {
year = digit; year = digit;
} else { } else {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -464,8 +260,7 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
} else { } else {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} else if (Character.isSpace(next) || next == ',' } else if (Character.isSpace(next) || next == ',' || next == '-' || next == '\r') {
|| next == '-' || next == '\r') {
if (hour != -1 && minute == -1) { if (hour != -1 && minute == -1) {
minute = digit; minute = digit;
} else if (minute != -1 && second == -1) { } else if (minute != -1 && second == -1) {
@ -502,15 +297,12 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
} }
hour += 12; hour += 12;
} else { } else {
DateFormatSymbols symbols = new DateFormatSymbols(Locale.US); DateFormatSymbols symbols = new DateFormatSymbols(TLocale.US);
String[] weekdays = symbols.getWeekdays(), months = symbols String[] weekdays = symbols.getWeekdays(), months = symbols.getMonths();
.getMonths();
int value; int value;
if (parse(text, weekdays) != -1) {/* empty */ if (parse(text, weekdays) != -1) {/* empty */
} else if (month == -1 } else if (month == -1 && (month = parse(text, months)) != -1) {/* empty */
&& (month = parse(text, months)) != -1) {/* empty */ } else if (text.equals("GMT") || text.equals("UT") || text.equals("UTC")) {
} else if (text.equals("GMT") || text.equals("UT") //$NON-NLS-1$ //$NON-NLS-2$
|| text.equals("UTC")) { //$NON-NLS-1$
zone = true; zone = true;
zoneOffset = 0; zoneOffset = 0;
} else if ((value = zone(text)) != 0) { } else if ((value = zone(text)) != 0) {
@ -524,8 +316,7 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
if (next == '+' || (year != -1 && next == '-')) { if (next == '+' || (year != -1 && next == '-')) {
sign = next; sign = next;
} else if (!Character.isSpace(next) && next != ',' } else if (!Character.isSpace(next) && next != ',' && nextState != NUMBERS) {
&& nextState != NUMBERS) {
sign = 0; sign = 0;
} }
@ -560,20 +351,11 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
} }
return UTC(year - 1900, month, date, hour, minute, second); return UTC(year - 1900, month, date, hour, minute, second);
} }
return new Date(year - 1900, month, date, hour, minute, second) return new Date(year - 1900, month, date, hour, minute, second).getTime();
.getTime();
} }
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
/**
* Sets the gregorian calendar day of the month for this {@code Date} object.
*
* @param day
* the day of the month.
*
* @deprecated use {@code Calendar.set(Calendar.DATE, day)}
*/
@Deprecated @Deprecated
public void setDate(int day) { public void setDate(int day) {
GregorianCalendar cal = new GregorianCalendar(milliseconds); GregorianCalendar cal = new GregorianCalendar(milliseconds);
@ -581,14 +363,6 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Sets the gregorian calendar hour of the day for this {@code Date} object.
*
* @param hour
* the hour of the day.
*
* @deprecated use {@code Calendar.set(Calendar.HOUR_OF_DAY, hour)}
*/
@Deprecated @Deprecated
public void setHours(int hour) { public void setHours(int hour) {
GregorianCalendar cal = new GregorianCalendar(milliseconds); GregorianCalendar cal = new GregorianCalendar(milliseconds);
@ -596,14 +370,6 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Sets the gregorian calendar minute of the hour for this {@code Date} object.
*
* @param minute
* the minutes.
*
* @deprecated use {@code Calendar.set(Calendar.MINUTE, minute)}
*/
@Deprecated @Deprecated
public void setMinutes(int minute) { public void setMinutes(int minute) {
GregorianCalendar cal = new GregorianCalendar(milliseconds); GregorianCalendar cal = new GregorianCalendar(milliseconds);
@ -611,14 +377,6 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Sets the gregorian calendar month for this {@code Date} object.
*
* @param month
* the month.
*
* @deprecated use {@code Calendar.set(Calendar.MONTH, month)}
*/
@Deprecated @Deprecated
public void setMonth(int month) { public void setMonth(int month) {
GregorianCalendar cal = new GregorianCalendar(milliseconds); GregorianCalendar cal = new GregorianCalendar(milliseconds);
@ -626,14 +384,6 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Sets the gregorian calendar second of the minute for this {@code Date} object.
*
* @param second
* the seconds.
*
* @deprecated use {@code Calendar.set(Calendar.SECOND, second)}
*/
@Deprecated @Deprecated
public void setSeconds(int second) { public void setSeconds(int second) {
GregorianCalendar cal = new GregorianCalendar(milliseconds); GregorianCalendar cal = new GregorianCalendar(milliseconds);
@ -641,25 +391,10 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Sets this {@code Date} to the specified millisecond value. The value is the
* number of milliseconds since Jan. 1, 1970 GMT.
*
* @param milliseconds
* the number of milliseconds since Jan. 1, 1970 GMT.
*/
public void setTime(long milliseconds) { public void setTime(long milliseconds) {
this.milliseconds = milliseconds; this.milliseconds = milliseconds;
} }
/**
* Sets the gregorian calendar year since 1900 for this {@code Date} object.
*
* @param year
* the year since 1900.
*
* @deprecated use {@code Calendar.set(Calendar.YEAR, year + 1900)}
*/
@Deprecated @Deprecated
public void setYear(int year) { public void setYear(int year) {
GregorianCalendar cal = new GregorianCalendar(milliseconds); GregorianCalendar cal = new GregorianCalendar(milliseconds);
@ -667,52 +402,28 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
milliseconds = cal.getTimeInMillis(); milliseconds = cal.getTimeInMillis();
} }
/**
* Returns the string representation of this {@code Date} in GMT in the format: 22
* Jun 1999 13:02:00 GMT
*
* @return the string representation of this {@code Date} in GMT.
*
* @deprecated use {@link DateFormat}
*/
@Deprecated @Deprecated
public String toGMTString() { public String toGMTString() {
SimpleDateFormat format1 = new SimpleDateFormat("d MMM ", Locale.US); //$NON-NLS-1$ SimpleDateFormat format1 = new SimpleDateFormat("d MMM ", TLocale.US);
SimpleDateFormat format2 = new SimpleDateFormat( SimpleDateFormat format2 = new SimpleDateFormat(" HH:mm:ss 'GMT'", TLocale.US);
" HH:mm:ss 'GMT'", Locale.US); //$NON-NLS-1$ TimeZone gmtZone = TimeZone.getTimeZone("GMT");
TimeZone gmtZone = TimeZone.getTimeZone("GMT"); //$NON-NLS-1$
format1.setTimeZone(gmtZone); format1.setTimeZone(gmtZone);
format2.setTimeZone(gmtZone); format2.setTimeZone(gmtZone);
GregorianCalendar gc = new GregorianCalendar(gmtZone); GregorianCalendar gc = new GregorianCalendar(gmtZone);
gc.setTimeInMillis(milliseconds); gc.setTimeInMillis(milliseconds);
return format1.format(this) + gc.get(Calendar.YEAR) return format1.format(this) + gc.get(Calendar.YEAR) + format2.format(this);
+ format2.format(this);
} }
/**
* Returns the string representation of this {@code Date} for the default {@code Locale}.
*
* @return the string representation of this {@code Date} for the default {@code Locale}.
*
* @deprecated use {@link DateFormat}
*/
@Deprecated @Deprecated
public String toLocaleString() { public String toLocaleString() {
return DateFormat.getDateTimeInstance().format(this); return DateFormat.getDateTimeInstance().format(this);
} }
/**
* Returns the string representation of this {@code Date} in the format: Tue Jun 22
* 13:07:00 GMT 1999
*
* @return the string representation of this {@code Date}.
*/
@Override @Override
public String toString() { public String toString() {
Calendar cal = new GregorianCalendar(milliseconds); Calendar cal = new GregorianCalendar(milliseconds);
TimeZone zone = cal.getTimeZone(); TimeZone zone = cal.getTimeZone();
String zoneName = zone.getDisplayName(zone.inDaylightTime(this), String zoneName = zone.getDisplayName(zone.inDaylightTime(this), TimeZone.SHORT, TLocale.getDefault());
TimeZone.SHORT, Locale.getDefault());
StringBuilder sb = new StringBuilder(34); StringBuilder sb = new StringBuilder(34);
sb.append(dayOfWeekNames[cal.get(Calendar.DAY_OF_WEEK) - 1]); sb.append(dayOfWeekNames[cal.get(Calendar.DAY_OF_WEEK) - 1]);
@ -735,80 +446,46 @@ public class Date implements TSerializable, TCloneable, TComparable<Date> {
} }
private String toTwoDigits(int digit) { private String toTwoDigits(int digit) {
if(digit >= 10) { if (digit >= 10) {
return "" + digit;//$NON-NLS-1$ return "" + digit;
} else { } else {
return "0" + digit;//$NON-NLS-1$ return "0" + digit;
} }
} }
/**
* Returns the millisecond value of the specified date and time in GMT.
*
* @param year
* the year, 0 is 1900.
* @param month
* the month, 0 - 11.
* @param day
* the day of the month, 1 - 31.
* @param hour
* the hour of day, 0 - 23.
* @param minute
* the minute of the hour, 0 - 59.
* @param second
* the second of the minute, 0 - 59.
* @return the date and time in GMT in milliseconds.
*
* @deprecated use: <code>
* Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
* cal.set(year + 1900, month, day, hour, minute, second);
* cal.getTime().getTime();</code>
*/
@Deprecated @Deprecated
public static long UTC(int year, int month, int day, int hour, int minute, public static long UTC(int year, int month, int day, int hour, int minute, int second) {
int second) {
GregorianCalendar cal = new GregorianCalendar(false); GregorianCalendar cal = new GregorianCalendar(false);
cal.setTimeZone(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$ cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.set(1900 + year, month, day, hour, minute, second); cal.set(1900 + year, month, day, hour, minute, second);
return cal.getTimeInMillis(); return cal.getTimeInMillis();
} }
private static int zone(String text) { private static int zone(String text) {
if (text.equals("EST")) { //$NON-NLS-1$ if (text.equals("EST")) {
return -5; return -5;
} }
if (text.equals("EDT")) { //$NON-NLS-1$ if (text.equals("EDT")) {
return -4; return -4;
} }
if (text.equals("CST")) { //$NON-NLS-1$ if (text.equals("CST")) {
return -6; return -6;
} }
if (text.equals("CDT")) { //$NON-NLS-1$ if (text.equals("CDT")) {
return -5; return -5;
} }
if (text.equals("MST")) { //$NON-NLS-1$ if (text.equals("MST")) {
return -7; return -7;
} }
if (text.equals("MDT")) { //$NON-NLS-1$ if (text.equals("MDT")) {
return -6; return -6;
} }
if (text.equals("PST")) { //$NON-NLS-1$ if (text.equals("PST")) {
return -8; return -8;
} }
if (text.equals("PDT")) { //$NON-NLS-1$ if (text.equals("PDT")) {
return -7; return -7;
} }
return 0; return 0;
} }
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
stream.writeLong(getTime());
}
private void readObject(ObjectInputStream stream) throws IOException,
ClassNotFoundException {
stream.defaultReadObject();
setTime(stream.readLong());
}
} }

View File

@ -17,174 +17,7 @@
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* {@code GregorianCalendar} is a concrete subclass of {@link Calendar}
* and provides the standard calendar used by most of the world.
*
* <p>
* The standard (Gregorian) calendar has 2 eras, BC and AD.
*
* <p>
* This implementation handles a single discontinuity, which corresponds by
* default to the date the Gregorian calendar was instituted (October 15, 1582
* in some countries, later in others). The cutover date may be changed by the
* caller by calling {@code setGregorianChange()}.
*
* <p>
* Historically, in those countries which adopted the Gregorian calendar first,
* October 4, 1582 was thus followed by October 15, 1582. This calendar models
* this correctly. Before the Gregorian cutover, {@code GregorianCalendar}
* implements the Julian calendar. The only difference between the Gregorian and
* the Julian calendar is the leap year rule. The Julian calendar specifies leap
* years every four years, whereas the Gregorian calendar omits century years
* which are not divisible by 400.
*
* <p>
* {@code GregorianCalendar} implements <em>proleptic</em> Gregorian
* and Julian calendars. That is, dates are computed by extrapolating the
* current rules indefinitely far backward and forward in time. As a result,
* {@code GregorianCalendar} may be used for all years to generate
* meaningful and consistent results. However, dates obtained using
* {@code GregorianCalendar} are historically accurate only from March 1,
* 4 AD onward, when modern Julian calendar rules were adopted. Before this
* date, leap year rules were applied irregularly, and before 45 BC the Julian
* calendar did not even exist.
*
* <p>
* Prior to the institution of the Gregorian calendar, New Year's Day was March
* 25. To avoid confusion, this calendar always uses January 1. A manual
* adjustment may be made if desired for dates that are prior to the Gregorian
* changeover and which fall between January 1 and March 24.
*
* <p>
* Values calculated for the {@code WEEK_OF_YEAR} field range from 1 to
* 53. Week 1 for a year is the earliest seven day period starting on
* {@code getFirstDayOfWeek()} that contains at least
* {@code getMinimalDaysInFirstWeek()} days from that year. It thus
* depends on the values of {@code getMinimalDaysInFirstWeek()},
* {@code getFirstDayOfWeek()}, and the day of the week of January 1.
* Weeks between week 1 of one year and week 1 of the following year are
* numbered sequentially from 2 to 52 or 53 (as needed).
*
* <p>
* For example, January 1, 1998 was a Thursday. If
* {@code getFirstDayOfWeek()} is {@code MONDAY} and
* {@code getMinimalDaysInFirstWeek()} is 4 (these are the values
* reflecting ISO 8601 and many national standards), then week 1 of 1998 starts
* on December 29, 1997, and ends on January 4, 1998. If, however,
* {@code getFirstDayOfWeek()} is {@code SUNDAY}, then week 1 of
* 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three
* days of 1998 then are part of week 53 of 1997.
*
* <p>
* Values calculated for the {@code WEEK_OF_MONTH} field range from 0 or
* 1 to 4 or 5. Week 1 of a month (the days with <code>WEEK_OF_MONTH =
* 1</code>)
* is the earliest set of at least {@code getMinimalDaysInFirstWeek()}
* contiguous days in that month, ending on the day before
* {@code getFirstDayOfWeek()}. Unlike week 1 of a year, week 1 of a
* month may be shorter than 7 days, need not start on
* {@code getFirstDayOfWeek()}, and will not include days of the
* previous month. Days of a month before week 1 have a
* {@code WEEK_OF_MONTH} of 0.
*
* <p>
* For example, if {@code getFirstDayOfWeek()} is {@code SUNDAY}
* and {@code getMinimalDaysInFirstWeek()} is 4, then the first week of
* January 1998 is Sunday, January 4 through Saturday, January 10. These days
* have a {@code WEEK_OF_MONTH} of 1. Thursday, January 1 through
* Saturday, January 3 have a {@code WEEK_OF_MONTH} of 0. If
* {@code getMinimalDaysInFirstWeek()} is changed to 3, then January 1
* through January 3 have a {@code WEEK_OF_MONTH} of 1.
*
* <p>
* <strong>Example:</strong> <blockquote>
*
* <pre>
* // get the supported ids for GMT-08:00 (Pacific Standard Time)
* String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
* // if no ids were returned, something is wrong. get out.
* if (ids.length == 0)
* System.exit(0);
*
* // begin output
* System.out.println("Current Time");
*
* // create a Pacific Standard Time time zone
* SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
*
* // set up rules for daylight savings time
* pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
* pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
*
* // create a GregorianCalendar with the Pacific Daylight time zone
* // and the current date and time
* Calendar calendar = new GregorianCalendar(pdt);
* Date trialTime = new Date();
* calendar.setTime(trialTime);
*
* // print out a bunch of interesting things
* System.out.println("ERA: " + calendar.get(Calendar.ERA));
* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
* System.out.println("DATE: " + calendar.get(Calendar.DATE));
* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
* System.out.println("DAY_OF_WEEK_IN_MONTH: "
* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
* System.out.println("ZONE_OFFSET: "
* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
* System.out.println("DST_OFFSET: "
* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
* System.out.println("Current Time, with hour reset to 3");
* calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
* calendar.set(Calendar.HOUR, 3);
* System.out.println("ERA: " + calendar.get(Calendar.ERA));
* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
* System.out.println("DATE: " + calendar.get(Calendar.DATE));
* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
* System.out.println("DAY_OF_WEEK_IN_MONTH: "
* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
* System.out.println("ZONE_OFFSET: "
* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
* System.out.println("DST_OFFSET: "
* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
* </pre>
*
* </blockquote>
*
* @see Calendar
* @see TimeZone
*/
public class GregorianCalendar extends Calendar { public class GregorianCalendar extends Calendar {
private static final long serialVersionUID = -8125100834729963327L;
/** /**
* Value for the BC era. * Value for the BC era.
*/ */
@ -236,7 +69,7 @@ public class GregorianCalendar extends Calendar {
* time with the default {@code Locale} and {@code TimeZone}. * time with the default {@code Locale} and {@code TimeZone}.
*/ */
public GregorianCalendar() { public GregorianCalendar() {
this(TimeZone.getDefault(), Locale.getDefault()); this(TimeZone.getDefault(), TLocale.getDefault());
} }
/** /**
@ -251,7 +84,7 @@ public class GregorianCalendar extends Calendar {
* the day of the month. * the day of the month.
*/ */
public GregorianCalendar(int year, int month, int day) { public GregorianCalendar(int year, int month, int day) {
super(TimeZone.getDefault(), Locale.getDefault()); super(TimeZone.getDefault(), TLocale.getDefault());
set(year, month, day); set(year, month, day);
} }
@ -271,7 +104,7 @@ public class GregorianCalendar extends Calendar {
* the minute. * the minute.
*/ */
public GregorianCalendar(int year, int month, int day, int hour, int minute) { public GregorianCalendar(int year, int month, int day, int hour, int minute) {
super(TimeZone.getDefault(), Locale.getDefault()); super(TimeZone.getDefault(), TLocale.getDefault());
set(year, month, day, hour, minute); set(year, month, day, hour, minute);
} }
@ -294,7 +127,7 @@ public class GregorianCalendar extends Calendar {
*/ */
public GregorianCalendar(int year, int month, int day, int hour, public GregorianCalendar(int year, int month, int day, int hour,
int minute, int second) { int minute, int second) {
super(TimeZone.getDefault(), Locale.getDefault()); super(TimeZone.getDefault(), TLocale.getDefault());
set(year, month, day, hour, minute, second); set(year, month, day, hour, minute, second);
} }
@ -310,7 +143,7 @@ public class GregorianCalendar extends Calendar {
* @param locale * @param locale
* the {@code Locale}. * the {@code Locale}.
*/ */
public GregorianCalendar(Locale locale) { public GregorianCalendar(TLocale locale) {
this(TimeZone.getDefault(), locale); this(TimeZone.getDefault(), locale);
} }
@ -322,7 +155,7 @@ public class GregorianCalendar extends Calendar {
* the {@code TimeZone}. * the {@code TimeZone}.
*/ */
public GregorianCalendar(TimeZone timezone) { public GregorianCalendar(TimeZone timezone) {
this(timezone, Locale.getDefault()); this(timezone, TLocale.getDefault());
} }
/** /**
@ -334,12 +167,12 @@ public class GregorianCalendar extends Calendar {
* @param locale * @param locale
* the {@code Locale}. * the {@code Locale}.
*/ */
public GregorianCalendar(TimeZone timezone, Locale locale) { public GregorianCalendar(TimeZone timezone, TLocale locale) {
super(timezone, locale); super(timezone, locale);
setTimeInMillis(System.currentTimeMillis()); setTimeInMillis(System.currentTimeMillis());
} }
GregorianCalendar(boolean ignored) { GregorianCalendar(@SuppressWarnings("unused") boolean ignored) {
super(TimeZone.getDefault()); super(TimeZone.getDefault());
setFirstDayOfWeek(SUNDAY); setFirstDayOfWeek(SUNDAY);
setMinimalDaysInFirstWeek(1); setMinimalDaysInFirstWeek(1);
@ -1413,18 +1246,6 @@ public class GregorianCalendar extends Calendar {
isCached = false; isCached = false;
} }
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
}
private void readObject(ObjectInputStream stream) throws IOException,
ClassNotFoundException {
stream.defaultReadObject();
setGregorianChange(new Date(gregorianCutover));
isCached = false;
}
@Override @Override
public void setFirstDayOfWeek(int value) { public void setFirstDayOfWeek(int value) {
super.setFirstDayOfWeek(value); super.setFirstDayOfWeek(value);

View File

@ -17,33 +17,8 @@
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.apache.harmony.luni.internal.nls.Messages;
/**
* {@code SimpleTimeZone} is a concrete subclass of {@code TimeZone}
* that represents a time zone for use with a Gregorian calendar. This class
* does not handle historical changes.
* <p>
* Use a negative value for {@code dayOfWeekInMonth} to indicate that
* {@code SimpleTimeZone} should count from the end of the month
* backwards. For example, Daylight Savings Time ends at the last
* (dayOfWeekInMonth = -1) Sunday in October, at 2 AM in standard time.
*
* @see Calendar
* @see GregorianCalendar
* @see TimeZone
*/
public class SimpleTimeZone extends TimeZone { public class SimpleTimeZone extends TimeZone {
private static final long serialVersionUID = -403250971215465050L;
private int rawOffset; private int rawOffset;
private int startYear, startMonth, startDay, startDayOfWeek, startTime; private int startYear, startMonth, startDay, startDayOfWeek, startTime;
@ -52,26 +27,12 @@ public class SimpleTimeZone extends TimeZone {
private int startMode, endMode; private int startMode, endMode;
private static final int DOM_MODE = 1, DOW_IN_MONTH_MODE = 2, private static final int DOM_MODE = 1, DOW_IN_MONTH_MODE = 2, DOW_GE_DOM_MODE = 3, DOW_LE_DOM_MODE = 4;
DOW_GE_DOM_MODE = 3, DOW_LE_DOM_MODE = 4;
/**
* The constant for representing a start or end time in GMT time mode.
*/
public static final int UTC_TIME = 2; public static final int UTC_TIME = 2;
/**
* The constant for representing a start or end time in standard local time mode,
* based on timezone's raw offset from GMT; does not include Daylight
* savings.
*/
public static final int STANDARD_TIME = 1; public static final int STANDARD_TIME = 1;
/**
* The constant for representing a start or end time in local wall clock time
* mode, based on timezone's adjusted offset from GMT; includes
* Daylight savings.
*/
public static final int WALL_TIME = 0; public static final int WALL_TIME = 0;
private boolean useDaylight; private boolean useDaylight;
@ -80,22 +41,8 @@ public class SimpleTimeZone extends TimeZone {
private int dstSavings = 3600000; private int dstSavings = 3600000;
private final transient com.ibm.icu.util.TimeZone icuTZ;
private final transient boolean isSimple; private final transient boolean isSimple;
/**
* Constructs a {@code SimpleTimeZone} with the given base time zone offset from GMT
* and time zone ID. Timezone IDs can be obtained from
* {@code TimeZone.getAvailableIDs}. Normally you should use {@code TimeZone.getDefault} to
* construct a {@code TimeZone}.
*
* @param offset
* the given base time zone offset to GMT.
* @param name
* the time zone ID which is obtained from
* {@code TimeZone.getAvailableIDs}.
*/
public SimpleTimeZone(int offset, final String name) { public SimpleTimeZone(int offset, final String name) {
setID(name); setID(name);
rawOffset = offset; rawOffset = offset;
@ -109,131 +56,18 @@ public class SimpleTimeZone extends TimeZone {
useDaylight = icuTZ.useDaylightTime(); useDaylight = icuTZ.useDaylightTime();
} }
/** public SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime,
* Constructs a {@code SimpleTimeZone} with the given base time zone offset from GMT, int endMonth, int endDay, int endDayOfWeek, int endTime) {
* time zone ID, and times to start and end the daylight savings time. Timezone IDs can this(offset, name, startMonth, startDay, startDayOfWeek, startTime, endMonth, endDay, endDayOfWeek, endTime,
* be obtained from {@code TimeZone.getAvailableIDs}. Normally you should use 3600000);
* {@code TimeZone.getDefault} to create a {@code TimeZone}. For a time zone that does not
* use daylight saving time, do not use this constructor; instead you should
* use {@code SimpleTimeZone(rawOffset, ID)}.
* <p>
* By default, this constructor specifies day-of-week-in-month rules. That
* is, if the {@code startDay} is 1, and the {@code startDayOfWeek} is {@code SUNDAY}, then this
* indicates the first Sunday in the {@code startMonth}. A {@code startDay} of -1 likewise
* indicates the last Sunday. However, by using negative or zero values for
* certain parameters, other types of rules can be specified.
* <p>
* Day of month: To specify an exact day of the month, such as March 1, set
* {@code startDayOfWeek} to zero.
* <p>
* Day of week after day of month: To specify the first day of the week
* occurring on or after an exact day of the month, make the day of the week
* negative. For example, if {@code startDay} is 5 and {@code startDayOfWeek} is {@code -MONDAY},
* this indicates the first Monday on or after the 5th day of the
* {@code startMonth}.
* <p>
* Day of week before day of month: To specify the last day of the week
* occurring on or before an exact day of the month, make the day of the
* week and the day of the month negative. For example, if {@code startDay} is {@code -21}
* and {@code startDayOfWeek} is {@code -WEDNESDAY}, this indicates the last Wednesday on or
* before the 21st of the {@code startMonth}.
* <p>
* The above examples refer to the {@code startMonth}, {@code startDay}, and {@code startDayOfWeek};
* the same applies for the {@code endMonth}, {@code endDay}, and {@code endDayOfWeek}.
* <p>
* The daylight savings time difference is set to the default value: one hour.
*
* @param offset
* the given base time zone offset to GMT.
* @param name
* the time zone ID which is obtained from
* {@code TimeZone.getAvailableIDs}.
* @param startMonth
* the daylight savings starting month. The month indexing is 0-based. eg, 0
* for January.
* @param startDay
* the daylight savings starting day-of-week-in-month. Please see
* the member description for an example.
* @param startDayOfWeek
* the daylight savings starting day-of-week. Please see the
* member description for an example.
* @param startTime
* the daylight savings starting time in local wall time, which
* is standard time in this case. Please see the member
* description for an example.
* @param endMonth
* the daylight savings ending month. The month indexing is 0-based. eg, 0 for
* January.
* @param endDay
* the daylight savings ending day-of-week-in-month. Please see
* the member description for an example.
* @param endDayOfWeek
* the daylight savings ending day-of-week. Please see the member
* description for an example.
* @param endTime
* the daylight savings ending time in local wall time, which is
* daylight time in this case. Please see the member description
* for an example.
* @throws IllegalArgumentException
* if the month, day, dayOfWeek, or time parameters are out of
* range for the start or end rule.
*/
public SimpleTimeZone(int offset, String name, int startMonth,
int startDay, int startDayOfWeek, int startTime, int endMonth,
int endDay, int endDayOfWeek, int endTime) {
this(offset, name, startMonth, startDay, startDayOfWeek, startTime,
endMonth, endDay, endDayOfWeek, endTime, 3600000);
} }
/** public SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime,
* Constructs a {@code SimpleTimeZone} with the given base time zone offset from GMT, int endMonth, int endDay, int endDayOfWeek, int endTime, int daylightSavings) {
* time zone ID, times to start and end the daylight savings time, and
* the daylight savings time difference in milliseconds.
*
* @param offset
* the given base time zone offset to GMT.
* @param name
* the time zone ID which is obtained from
* {@code TimeZone.getAvailableIDs}.
* @param startMonth
* the daylight savings starting month. Month is 0-based. eg, 0
* for January.
* @param startDay
* the daylight savings starting day-of-week-in-month. Please see
* the description of {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)} for an example.
* @param startDayOfWeek
* the daylight savings starting day-of-week. Please see the
* description of {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)} for an example.
* @param startTime
* The daylight savings starting time in local wall time, which
* is standard time in this case. Please see the description of
* {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)} for an example.
* @param endMonth
* the daylight savings ending month. Month is 0-based. eg, 0 for
* January.
* @param endDay
* the daylight savings ending day-of-week-in-month. Please see
* the description of {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)} for an example.
* @param endDayOfWeek
* the daylight savings ending day-of-week. Please see the description of
* {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)} for an example.
* @param endTime
* the daylight savings ending time in local wall time, which is
* daylight time in this case. Please see the description of {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)}
* for an example.
* @param daylightSavings
* the daylight savings time difference in milliseconds.
* @throws IllegalArgumentException
* if the month, day, dayOfWeek, or time parameters are out of
* range for the start or end rule.
*/
public SimpleTimeZone(int offset, String name, int startMonth,
int startDay, int startDayOfWeek, int startTime, int endMonth,
int endDay, int endDayOfWeek, int endTime, int daylightSavings) {
icuTZ = getICUTimeZone(name); icuTZ = getICUTimeZone(name);
if (icuTZ instanceof com.ibm.icu.util.SimpleTimeZone) { if (icuTZ instanceof com.ibm.icu.util.SimpleTimeZone) {
isSimple = true; isSimple = true;
com.ibm.icu.util.SimpleTimeZone tz = (com.ibm.icu.util.SimpleTimeZone)icuTZ; com.ibm.icu.util.SimpleTimeZone tz = (com.ibm.icu.util.SimpleTimeZone) icuTZ;
tz.setRawOffset(offset); tz.setRawOffset(offset);
tz.setStartRule(startMonth, startDay, startDayOfWeek, startTime); tz.setStartRule(startMonth, startDay, startDayOfWeek, startTime);
tz.setEndRule(endMonth, endDay, endDayOfWeek, endTime); tz.setEndRule(endMonth, endDay, endDayOfWeek, endTime);
@ -244,8 +78,7 @@ public class SimpleTimeZone extends TimeZone {
setID(name); setID(name);
rawOffset = offset; rawOffset = offset;
if (daylightSavings <= 0) { if (daylightSavings <= 0) {
throw new IllegalArgumentException(Messages.getString( throw new IllegalArgumentException(String.valueOf(daylightSavings));
"luni.3B", daylightSavings)); //$NON-NLS-1$
} }
dstSavings = daylightSavings; dstSavings = daylightSavings;
@ -255,70 +88,16 @@ public class SimpleTimeZone extends TimeZone {
useDaylight = daylightSavings > 0 || icuTZ.useDaylightTime(); useDaylight = daylightSavings > 0 || icuTZ.useDaylightTime();
} }
/** public SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime,
* Construct a {@code SimpleTimeZone} with the given base time zone offset from GMT, int startTimeMode, int endMonth, int endDay, int endDayOfWeek, int endTime, int endTimeMode,
* time zone ID, times to start and end the daylight savings time including a int daylightSavings) {
* mode specifier, the daylight savings time difference in milliseconds.
* The mode specifies either {@link #WALL_TIME}, {@link #STANDARD_TIME}, or
* {@link #UTC_TIME}.
*
* @param offset
* the given base time zone offset to GMT.
* @param name
* the time zone ID which is obtained from
* {@code TimeZone.getAvailableIDs}.
* @param startMonth
* the daylight savings starting month. The month indexing is 0-based. eg, 0
* for January.
* @param startDay
* the daylight savings starting day-of-week-in-month. Please see
* the description of {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)} for an example.
* @param startDayOfWeek
* the daylight savings starting day-of-week. Please see the
* description of {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)} for an example.
* @param startTime
* the time of day in milliseconds on which daylight savings
* time starts, based on the {@code startTimeMode}.
* @param startTimeMode
* the mode (UTC, standard, or wall time) of the start time
* value.
* @param endDay
* the day of the week on which daylight savings time ends.
* @param endMonth
* the daylight savings ending month. The month indexing is 0-based. eg, 0 for
* January.
* @param endDayOfWeek
* the daylight savings ending day-of-week. Please see the description of
* {@link #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)} for an example.
* @param endTime
* the time of day in milliseconds on which daylight savings
* time ends, based on the {@code endTimeMode}.
* @param endTimeMode
* the mode (UTC, standard, or wall time) of the end time value.
* @param daylightSavings
* the daylight savings time difference in milliseconds.
* @throws IllegalArgumentException
* if the month, day, dayOfWeek, or time parameters are out of
* range for the start or end rule.
*/
public SimpleTimeZone(int offset, String name, int startMonth,
int startDay, int startDayOfWeek, int startTime, int startTimeMode,
int endMonth, int endDay, int endDayOfWeek, int endTime,
int endTimeMode, int daylightSavings) {
this(offset, name, startMonth, startDay, startDayOfWeek, startTime, this(offset, name, startMonth, startDay, startDayOfWeek, startTime, endMonth, endDay, endDayOfWeek, endTime,
endMonth, endDay, endDayOfWeek, endTime, daylightSavings); daylightSavings);
startMode = startTimeMode; startMode = startTimeMode;
endMode = endTimeMode; endMode = endTimeMode;
} }
/**
* Returns a new {@code SimpleTimeZone} with the same ID, {@code rawOffset} and daylight
* savings time rules as this SimpleTimeZone.
*
* @return a shallow copy of this {@code SimpleTimeZone}.
* @see java.lang.Cloneable
*/
@Override @Override
public Object clone() { public Object clone() {
SimpleTimeZone zone = (SimpleTimeZone) super.clone(); SimpleTimeZone zone = (SimpleTimeZone) super.clone();
@ -328,34 +107,19 @@ public class SimpleTimeZone extends TimeZone {
return zone; return zone;
} }
/**
* Compares the specified object to this {@code SimpleTimeZone} and returns whether they
* are equal. The object must be an instance of {@code SimpleTimeZone} and have the
* same internal data.
*
* @param object
* the object to compare with this object.
* @return {@code true} if the specified object is equal to this
* {@code SimpleTimeZone}, {@code false} otherwise.
* @see #hashCode
*/
@Override @Override
public boolean equals(Object object) { public boolean equals(Object object) {
if (!(object instanceof SimpleTimeZone)) { if (!(object instanceof SimpleTimeZone)) {
return false; return false;
} }
SimpleTimeZone tz = (SimpleTimeZone) object; SimpleTimeZone tz = (SimpleTimeZone) object;
return getID().equals(tz.getID()) return getID().equals(tz.getID()) &&
&& rawOffset == tz.rawOffset rawOffset == tz.rawOffset &&
&& useDaylight == tz.useDaylight useDaylight == tz.useDaylight &&
&& (!useDaylight || (startYear == tz.startYear (!useDaylight || (startYear == tz.startYear && startMonth == tz.startMonth && startDay == tz.startDay &&
&& startMonth == tz.startMonth startMode == tz.startMode && startDayOfWeek == tz.startDayOfWeek && startTime == tz.startTime &&
&& startDay == tz.startDay && startMode == tz.startMode endMonth == tz.endMonth && endDay == tz.endDay && endDayOfWeek == tz.endDayOfWeek &&
&& startDayOfWeek == tz.startDayOfWeek endTime == tz.endTime && endMode == tz.endMode && dstSavings == tz.dstSavings));
&& startTime == tz.startTime && endMonth == tz.endMonth
&& endDay == tz.endDay
&& endDayOfWeek == tz.endDayOfWeek
&& endTime == tz.endTime && endMode == tz.endMode && dstSavings == tz.dstSavings));
} }
@Override @Override
@ -367,10 +131,9 @@ public class SimpleTimeZone extends TimeZone {
} }
@Override @Override
public int getOffset(int era, int year, int month, int day, int dayOfWeek, public int getOffset(int era, int year, int month, int day, int dayOfWeek, int time) {
int time) {
if (era != GregorianCalendar.BC && era != GregorianCalendar.AD) { if (era != GregorianCalendar.BC && era != GregorianCalendar.AD) {
throw new IllegalArgumentException(Messages.getString("luni.3C", era)); //$NON-NLS-1$ throw new IllegalArgumentException(String.valueOf(era));
} }
checkRange(month, dayOfWeek, time); checkRange(month, dayOfWeek, time);
if (month != Calendar.FEBRUARY || day != 29 || !isLeapYear(year)) { if (month != Calendar.FEBRUARY || day != 29 || !isLeapYear(year)) {
@ -389,20 +152,12 @@ public class SimpleTimeZone extends TimeZone {
return rawOffset; return rawOffset;
} }
/**
* Returns an integer hash code for the receiver. Objects which are equal
* return the same value for this method.
*
* @return the receiver's hash.
* @see #equals
*/
@Override @Override
public synchronized int hashCode() { public synchronized int hashCode() {
int hashCode = getID().hashCode() + rawOffset; int hashCode = getID().hashCode() + rawOffset;
if (useDaylight) { if (useDaylight) {
hashCode += startYear + startMonth + startDay + startDayOfWeek hashCode += startYear + startMonth + startDay + startDayOfWeek + startTime + startMode + endMonth + endDay +
+ startTime + startMode + endMonth + endDay + endDayOfWeek endDayOfWeek + endTime + endMode + dstSavings;
+ endTime + endMode + dstSavings;
} }
return hashCode; return hashCode;
} }
@ -419,13 +174,11 @@ public class SimpleTimeZone extends TimeZone {
if (!useDaylight) { if (!useDaylight) {
return rawOffset == tz.rawOffset; return rawOffset == tz.rawOffset;
} }
return rawOffset == tz.rawOffset && dstSavings == tz.dstSavings return rawOffset == tz.rawOffset && dstSavings == tz.dstSavings && startYear == tz.startYear &&
&& startYear == tz.startYear && startMonth == tz.startMonth startMonth == tz.startMonth && startDay == tz.startDay && startMode == tz.startMode &&
&& startDay == tz.startDay && startMode == tz.startMode startDayOfWeek == tz.startDayOfWeek && startTime == tz.startTime && endMonth == tz.endMonth &&
&& startDayOfWeek == tz.startDayOfWeek endDay == tz.endDay && endDayOfWeek == tz.endDayOfWeek && endTime == tz.endTime &&
&& startTime == tz.startTime && endMonth == tz.endMonth endMode == tz.endMode;
&& endDay == tz.endDay && endDayOfWeek == tz.endDayOfWeek
&& endTime == tz.endTime && endMode == tz.endMode;
} }
@Override @Override
@ -440,12 +193,6 @@ public class SimpleTimeZone extends TimeZone {
return year % 4 == 0; return year % 4 == 0;
} }
/**
* Sets the daylight savings offset in milliseconds for this {@code SimpleTimeZone}.
*
* @param milliseconds
* the daylight savings offset in milliseconds.
*/
public void setDSTSavings(int milliseconds) { public void setDSTSavings(int milliseconds) {
if (milliseconds > 0) { if (milliseconds > 0) {
dstSavings = milliseconds; dstSavings = milliseconds;
@ -456,20 +203,19 @@ public class SimpleTimeZone extends TimeZone {
private void checkRange(int month, int dayOfWeek, int time) { private void checkRange(int month, int dayOfWeek, int time) {
if (month < Calendar.JANUARY || month > Calendar.DECEMBER) { if (month < Calendar.JANUARY || month > Calendar.DECEMBER) {
throw new IllegalArgumentException(Messages.getString("luni.3D", month)); //$NON-NLS-1$ throw new IllegalArgumentException(String.valueOf(month));
} }
if (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY) { if (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY) {
throw new IllegalArgumentException(Messages throw new IllegalArgumentException(String.valueOf(dayOfWeek));
.getString("luni.48", dayOfWeek)); //$NON-NLS-1$
} }
if (time < 0 || time >= 24 * 3600000) { if (time < 0 || time >= 24 * 3600000) {
throw new IllegalArgumentException(Messages.getString("luni.3E", time)); //$NON-NLS-1$ throw new IllegalArgumentException(String.valueOf(time));
} }
} }
private void checkDay(int month, int day) { private void checkDay(int month, int day) {
if (day <= 0 || day > GregorianCalendar.DaysInMonth[month]) { if (day <= 0 || day > GregorianCalendar.DaysInMonth[month]) {
throw new IllegalArgumentException(Messages.getString("luni.3F", day)); //$NON-NLS-1$ throw new IllegalArgumentException(String.valueOf(day));
} }
} }
@ -489,14 +235,12 @@ public class SimpleTimeZone extends TimeZone {
} }
useDaylight = startDay != 0 && endDay != 0; useDaylight = startDay != 0 && endDay != 0;
if (endDay != 0) { if (endDay != 0) {
checkRange(endMonth, endMode == DOM_MODE ? 1 : endDayOfWeek, checkRange(endMonth, endMode == DOM_MODE ? 1 : endDayOfWeek, endTime);
endTime);
if (endMode != DOW_IN_MONTH_MODE) { if (endMode != DOW_IN_MONTH_MODE) {
checkDay(endMonth, endDay); checkDay(endMonth, endDay);
} else { } else {
if (endDay < -5 || endDay > 5) { if (endDay < -5 || endDay > 5) {
throw new IllegalArgumentException(Messages.getString( throw new IllegalArgumentException(Messages.getString("luni.40", endDay)); //$NON-NLS-1$
"luni.40", endDay)); //$NON-NLS-1$
} }
} }
} }
@ -505,18 +249,6 @@ public class SimpleTimeZone extends TimeZone {
} }
} }
/**
* Sets the rule which specifies the end of daylight savings time.
*
* @param month
* the {@code Calendar} month in which daylight savings time ends.
* @param dayOfMonth
* the {@code Calendar} day of the month on which daylight savings time
* ends.
* @param time
* the time of day in milliseconds standard time on which
* daylight savings time ends.
*/
public void setEndRule(int month, int dayOfMonth, int time) { public void setEndRule(int month, int dayOfMonth, int time) {
endMonth = month; endMonth = month;
endDay = dayOfMonth; endDay = dayOfMonth;
@ -524,26 +256,10 @@ public class SimpleTimeZone extends TimeZone {
endTime = time; endTime = time;
setEndMode(); setEndMode();
if (isSimple) { if (isSimple) {
((com.ibm.icu.util.SimpleTimeZone) icuTZ).setEndRule(month, ((com.ibm.icu.util.SimpleTimeZone) icuTZ).setEndRule(month, dayOfMonth, time);
dayOfMonth, time);
} }
} }
/**
* Sets the rule which specifies the end of daylight savings time.
*
* @param month
* the {@code Calendar} month in which daylight savings time ends.
* @param day
* the occurrence of the day of the week on which daylight
* savings time ends.
* @param dayOfWeek
* the {@code Calendar} day of the week on which daylight savings time
* ends.
* @param time
* the time of day in milliseconds standard time on which
* daylight savings time ends.
*/
public void setEndRule(int month, int day, int dayOfWeek, int time) { public void setEndRule(int month, int day, int dayOfWeek, int time) {
endMonth = month; endMonth = month;
endDay = day; endDay = day;
@ -551,46 +267,21 @@ public class SimpleTimeZone extends TimeZone {
endTime = time; endTime = time;
setEndMode(); setEndMode();
if (isSimple) { if (isSimple) {
((com.ibm.icu.util.SimpleTimeZone) icuTZ).setEndRule(month, day, ((com.ibm.icu.util.SimpleTimeZone) icuTZ).setEndRule(month, day, dayOfWeek, time);
dayOfWeek, time);
} }
} }
/** public void setEndRule(int month, int day, int dayOfWeek, int time, boolean after) {
* Sets the rule which specifies the end of daylight savings time.
*
* @param month
* the {@code Calendar} month in which daylight savings time ends.
* @param day
* the {@code Calendar} day of the month.
* @param dayOfWeek
* the {@code Calendar} day of the week on which daylight savings time
* ends.
* @param time
* the time of day in milliseconds on which daylight savings time
* ends.
* @param after
* selects the day after or before the day of month.
*/
public void setEndRule(int month, int day, int dayOfWeek, int time,
boolean after) {
endMonth = month; endMonth = month;
endDay = after ? day : -day; endDay = after ? day : -day;
endDayOfWeek = -dayOfWeek; endDayOfWeek = -dayOfWeek;
endTime = time; endTime = time;
setEndMode(); setEndMode();
if (isSimple) { if (isSimple) {
((com.ibm.icu.util.SimpleTimeZone) icuTZ).setEndRule(month, day, ((com.ibm.icu.util.SimpleTimeZone) icuTZ).setEndRule(month, day, dayOfWeek, time, after);
dayOfWeek, time, after);
} }
} }
/**
* Sets the offset for standard time from GMT for this {@code SimpleTimeZone}.
*
* @param offset
* the offset from GMT of standard time in milliseconds.
*/
@Override @Override
public void setRawOffset(int offset) { public void setRawOffset(int offset) {
rawOffset = offset; rawOffset = offset;
@ -613,14 +304,12 @@ public class SimpleTimeZone extends TimeZone {
} }
useDaylight = startDay != 0 && endDay != 0; useDaylight = startDay != 0 && endDay != 0;
if (startDay != 0) { if (startDay != 0) {
checkRange(startMonth, startMode == DOM_MODE ? 1 : startDayOfWeek, checkRange(startMonth, startMode == DOM_MODE ? 1 : startDayOfWeek, startTime);
startTime);
if (startMode != DOW_IN_MONTH_MODE) { if (startMode != DOW_IN_MONTH_MODE) {
checkDay(startMonth, startDay); checkDay(startMonth, startDay);
} else { } else {
if (startDay < -5 || startDay > 5) { if (startDay < -5 || startDay > 5) {
throw new IllegalArgumentException(Messages.getString( throw new IllegalArgumentException(Messages.getString("luni.40", startDay)); //$NON-NLS-1$
"luni.40", startDay)); //$NON-NLS-1$
} }
} }
} }
@ -629,18 +318,6 @@ public class SimpleTimeZone extends TimeZone {
} }
} }
/**
* Sets the rule which specifies the start of daylight savings time.
*
* @param month
* the {@code Calendar} month in which daylight savings time starts.
* @param dayOfMonth
* the {@code Calendar} day of the month on which daylight savings time
* starts.
* @param time
* the time of day in milliseconds on which daylight savings time
* starts.
*/
public void setStartRule(int month, int dayOfMonth, int time) { public void setStartRule(int month, int dayOfMonth, int time) {
startMonth = month; startMonth = month;
startDay = dayOfMonth; startDay = dayOfMonth;
@ -648,26 +325,10 @@ public class SimpleTimeZone extends TimeZone {
startTime = time; startTime = time;
setStartMode(); setStartMode();
if (isSimple) { if (isSimple) {
((com.ibm.icu.util.SimpleTimeZone) icuTZ).setStartRule(month, ((com.ibm.icu.util.SimpleTimeZone) icuTZ).setStartRule(month, dayOfMonth, time);
dayOfMonth, time);
} }
} }
/**
* Sets the rule which specifies the start of daylight savings time.
*
* @param month
* the {@code Calendar} month in which daylight savings time starts.
* @param day
* the occurrence of the day of the week on which daylight
* savings time starts.
* @param dayOfWeek
* the {@code Calendar} day of the week on which daylight savings time
* starts.
* @param time
* the time of day in milliseconds on which daylight savings time
* starts.
*/
public void setStartRule(int month, int day, int dayOfWeek, int time) { public void setStartRule(int month, int day, int dayOfWeek, int time) {
startMonth = month; startMonth = month;
startDay = day; startDay = day;
@ -675,199 +336,38 @@ public class SimpleTimeZone extends TimeZone {
startTime = time; startTime = time;
setStartMode(); setStartMode();
if (isSimple) { if (isSimple) {
((com.ibm.icu.util.SimpleTimeZone) icuTZ).setStartRule(month, day, ((com.ibm.icu.util.SimpleTimeZone) icuTZ).setStartRule(month, day, dayOfWeek, time);
dayOfWeek, time);
} }
} }
/** public void setStartRule(int month, int day, int dayOfWeek, int time, boolean after) {
* Sets the rule which specifies the start of daylight savings time.
*
* @param month
* the {@code Calendar} month in which daylight savings time starts.
* @param day
* the {@code Calendar} day of the month.
* @param dayOfWeek
* the {@code Calendar} day of the week on which daylight savings time
* starts.
* @param time
* the time of day in milliseconds on which daylight savings time
* starts.
* @param after
* selects the day after or before the day of month.
*/
public void setStartRule(int month, int day, int dayOfWeek, int time,
boolean after) {
startMonth = month; startMonth = month;
startDay = after ? day : -day; startDay = after ? day : -day;
startDayOfWeek = -dayOfWeek; startDayOfWeek = -dayOfWeek;
startTime = time; startTime = time;
setStartMode(); setStartMode();
if (isSimple) { if (isSimple) {
((com.ibm.icu.util.SimpleTimeZone) icuTZ).setStartRule(month, day, ((com.ibm.icu.util.SimpleTimeZone) icuTZ).setStartRule(month, day, dayOfWeek, time, after);
dayOfWeek, time, after);
} }
} }
/**
* Sets the starting year for daylight savings time in this {@code SimpleTimeZone}.
* Years before this start year will always be in standard time.
*
* @param year
* the starting year.
*/
public void setStartYear(int year) { public void setStartYear(int year) {
startYear = year; startYear = year;
useDaylight = true; useDaylight = true;
} }
/**
* Returns the string representation of this {@code SimpleTimeZone}.
*
* @return the string representation of this {@code SimpleTimeZone}.
*/
@Override @Override
public String toString() { public String toString() {
return getClass().getName() return getClass().getName() + "[id=" + getID() + ",offset=" + rawOffset + ",dstSavings=" + dstSavings +
+ "[id=" //$NON-NLS-1$ ",useDaylight=" + useDaylight + ",startYear=" + startYear + ",startMode=" + startMode + ",startMonth=" +
+ getID() startMonth + ",startDay=" + startDay + ",startDayOfWeek=" +
+ ",offset=" //$NON-NLS-1$ (useDaylight && (startMode != DOM_MODE) ? startDayOfWeek + 1 : 0) + ",startTime=" + startTime +
+ rawOffset ",endMode=" + endMode + ",endMonth=" + endMonth + ",endDay=" + endDay + ",endDayOfWeek=" +
+ ",dstSavings=" //$NON-NLS-1$ (useDaylight && (endMode != DOM_MODE) ? endDayOfWeek + 1 : 0) + ",endTime=" + endTime + "]";
+ dstSavings
+ ",useDaylight=" //$NON-NLS-1$
+ useDaylight
+ ",startYear=" //$NON-NLS-1$
+ startYear
+ ",startMode=" //$NON-NLS-1$
+ startMode
+ ",startMonth=" //$NON-NLS-1$
+ startMonth
+ ",startDay=" //$NON-NLS-1$
+ startDay
+ ",startDayOfWeek=" //$NON-NLS-1$
+ (useDaylight && (startMode != DOM_MODE) ? startDayOfWeek + 1
: 0) + ",startTime=" + startTime + ",endMode=" //$NON-NLS-1$ //$NON-NLS-2$
+ endMode + ",endMonth=" + endMonth + ",endDay=" + endDay //$NON-NLS-1$ //$NON-NLS-2$
+ ",endDayOfWeek=" //$NON-NLS-1$
+ (useDaylight && (endMode != DOM_MODE) ? endDayOfWeek + 1 : 0)
+ ",endTime=" + endTime + "]"; //$NON-NLS-1$//$NON-NLS-2$
} }
@Override @Override
public boolean useDaylightTime() { public boolean useDaylightTime() {
return useDaylight; return useDaylight;
} }
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("dstSavings", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("endDay", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("endDayOfWeek", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("endMode", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("endMonth", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("endTime", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("monthLength", byte[].class), //$NON-NLS-1$
new ObjectStreamField("rawOffset", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("serialVersionOnStream", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("startDay", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("startDayOfWeek", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("startMode", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("startMonth", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("startTime", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("startYear", Integer.TYPE), //$NON-NLS-1$
new ObjectStreamField("useDaylight", Boolean.TYPE), }; //$NON-NLS-1$
private void writeObject(ObjectOutputStream stream) throws IOException {
int sEndDay = endDay, sEndDayOfWeek = endDayOfWeek + 1, sStartDay = startDay, sStartDayOfWeek = startDayOfWeek + 1;
if (useDaylight
&& (startMode != DOW_IN_MONTH_MODE || endMode != DOW_IN_MONTH_MODE)) {
Calendar cal = new GregorianCalendar(this);
if (endMode != DOW_IN_MONTH_MODE) {
cal.set(Calendar.MONTH, endMonth);
cal.set(Calendar.DATE, endDay);
sEndDay = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
if (endMode == DOM_MODE) {
sEndDayOfWeek = cal.getFirstDayOfWeek();
}
}
if (startMode != DOW_IN_MONTH_MODE) {
cal.set(Calendar.MONTH, startMonth);
cal.set(Calendar.DATE, startDay);
sStartDay = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
if (startMode == DOM_MODE) {
sStartDayOfWeek = cal.getFirstDayOfWeek();
}
}
}
ObjectOutputStream.PutField fields = stream.putFields();
fields.put("dstSavings", dstSavings); //$NON-NLS-1$
fields.put("endDay", sEndDay); //$NON-NLS-1$
fields.put("endDayOfWeek", sEndDayOfWeek); //$NON-NLS-1$
fields.put("endMode", endMode); //$NON-NLS-1$
fields.put("endMonth", endMonth); //$NON-NLS-1$
fields.put("endTime", endTime); //$NON-NLS-1$
fields.put("monthLength", GregorianCalendar.DaysInMonth); //$NON-NLS-1$
fields.put("rawOffset", rawOffset); //$NON-NLS-1$
fields.put("serialVersionOnStream", 1); //$NON-NLS-1$
fields.put("startDay", sStartDay); //$NON-NLS-1$
fields.put("startDayOfWeek", sStartDayOfWeek); //$NON-NLS-1$
fields.put("startMode", startMode); //$NON-NLS-1$
fields.put("startMonth", startMonth); //$NON-NLS-1$
fields.put("startTime", startTime); //$NON-NLS-1$
fields.put("startYear", startYear); //$NON-NLS-1$
fields.put("useDaylight", useDaylight); //$NON-NLS-1$
stream.writeFields();
stream.writeInt(4);
byte[] values = new byte[4];
values[0] = (byte) startDay;
values[1] = (byte) (startMode == DOM_MODE ? 0 : startDayOfWeek + 1);
values[2] = (byte) endDay;
values[3] = (byte) (endMode == DOM_MODE ? 0 : endDayOfWeek + 1);
stream.write(values);
}
private void readObject(ObjectInputStream stream) throws IOException,
ClassNotFoundException {
ObjectInputStream.GetField fields = stream.readFields();
rawOffset = fields.get("rawOffset", 0); //$NON-NLS-1$
useDaylight = fields.get("useDaylight", false); //$NON-NLS-1$
if (useDaylight) {
endMonth = fields.get("endMonth", 0); //$NON-NLS-1$
endTime = fields.get("endTime", 0); //$NON-NLS-1$
startMonth = fields.get("startMonth", 0); //$NON-NLS-1$
startTime = fields.get("startTime", 0); //$NON-NLS-1$
startYear = fields.get("startYear", 0); //$NON-NLS-1$
}
if (fields.get("serialVersionOnStream", 0) == 0) { //$NON-NLS-1$
if (useDaylight) {
startMode = endMode = DOW_IN_MONTH_MODE;
endDay = fields.get("endDay", 0); //$NON-NLS-1$
endDayOfWeek = fields.get("endDayOfWeek", 0) - 1; //$NON-NLS-1$
startDay = fields.get("startDay", 0); //$NON-NLS-1$
startDayOfWeek = fields.get("startDayOfWeek", 0) - 1; //$NON-NLS-1$
}
} else {
dstSavings = fields.get("dstSavings", 0); //$NON-NLS-1$
if (useDaylight) {
endMode = fields.get("endMode", 0); //$NON-NLS-1$
startMode = fields.get("startMode", 0); //$NON-NLS-1$
int length = stream.readInt();
byte[] values = new byte[length];
stream.readFully(values);
if (length >= 4) {
startDay = values[0];
startDayOfWeek = values[1];
if (startMode != DOM_MODE) {
startDayOfWeek--;
}
endDay = values[2];
endDayOfWeek = values[3];
if (endMode != DOM_MODE) {
endDayOfWeek--;
}
}
}
}
}
} }

View File

@ -32,26 +32,14 @@
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.TCloneable; import org.teavm.classlib.java.lang.TCloneable;
public abstract class TimeZone implements TSerializable, TCloneable { public abstract class TimeZone implements TSerializable, TCloneable {
private static final long serialVersionUID = 3581463369166924961L; private static final long serialVersionUID = 3581463369166924961L;
/**
* The SHORT display name style.
*/
public static final int SHORT = 0; public static final int SHORT = 0;
/**
* The LONG display name style.
*/
public static final int LONG = 1; public static final int LONG = 1;
private static THashMap<String, TimeZone> AvailableZones; private static THashMap<String, TimeZone> AvailableZones;
@ -89,29 +77,16 @@ public abstract class TimeZone implements TSerializable, TCloneable {
} }
public static synchronized String[] getAvailableIDs(int offset) { public static synchronized String[] getAvailableIDs(int offset) {
List<String> result = new ArrayList<>(); TList<String> result = new TArrayList<>();
for (TIterator<TimeZone> iter = AvailableZones.values().iterator(); iter.hasNext()) { for (TIterator<TimeZone> iter = AvailableZones.values().iterator(); iter.hasNext();) {
if () TimeZone tz = iter.next();
}
return Arrays.copyOf(result, j);
int count = 0;
int length = availableIDs.length;
String[] all = new String[length];
for (int i = 0; i < length; i++) {
if (tz.getRawOffset() == offset) { if (tz.getRawOffset() == offset) {
all[count++] = tz.getID(); result.add(tz.getID());
} }
} }
String[] answer = new String[count]; return result.toArray(new String[0]);
System.arraycopy(all, 0, answer, 0, count);
return answer;
} }
/**
* Gets the default time zone.
*
* @return the default time zone.
*/
public static synchronized TimeZone getDefault() { public static synchronized TimeZone getDefault() {
if (Default == null) { if (Default == null) {
setDefault(null); setDefault(null);
@ -119,90 +94,29 @@ public abstract class TimeZone implements TSerializable, TCloneable {
return (TimeZone) Default.clone(); return (TimeZone) Default.clone();
} }
/**
* Gets the LONG name for this {@code TimeZone} for the default {@code Locale} in standard
* time. If the name is not available, the result is in the format
* {@code GMT[+-]hh:mm}.
*
* @return the {@code TimeZone} name.
*/
public final String getDisplayName() { public final String getDisplayName() {
return getDisplayName(false, LONG, Locale.getDefault()); return getDisplayName(false, LONG, TLocale.getDefault());
} }
/** public final String getDisplayName(TLocale locale) {
* Gets the LONG name for this {@code TimeZone} for the specified {@code Locale} in standard
* time. If the name is not available, the result is in the format
* {@code GMT[+-]hh:mm}.
*
* @param locale
* the {@code Locale}.
* @return the {@code TimeZone} name.
*/
public final String getDisplayName(Locale locale) {
return getDisplayName(false, LONG, locale); return getDisplayName(false, LONG, locale);
} }
/**
* Gets the specified style of name ({@code LONG} or {@code SHORT}) for this {@code TimeZone} for
* the default {@code Locale} in either standard or daylight time as specified. If
* the name is not available, the result is in the format {@code GMT[+-]hh:mm}.
*
* @param daylightTime
* {@code true} for daylight time, {@code false} for standard
* time.
* @param style
* either {@code LONG} or {@code SHORT}.
* @return the {@code TimeZone} name.
*/
public final String getDisplayName(boolean daylightTime, int style) { public final String getDisplayName(boolean daylightTime, int style) {
return getDisplayName(daylightTime, style, Locale.getDefault()); return getDisplayName(daylightTime, style, TLocale.getDefault());
} }
/** public String getDisplayName(boolean daylightTime, int style, TLocale locale) {
* Gets the specified style of name ({@code LONG} or {@code SHORT}) for this {@code TimeZone} for if (icuTimeZone == null || !ID.equals(icuTimeZone.getID())) {
* the specified {@code Locale} in either standard or daylight time as specified. If
* the name is not available, the result is in the format {@code GMT[+-]hh:mm}.
*
* @param daylightTime
* {@code true} for daylight time, {@code false} for standard
* time.
* @param style
* either LONG or SHORT.
* @param locale
* either {@code LONG} or {@code SHORT}.
* @return the {@code TimeZone} name.
*/
public String getDisplayName(boolean daylightTime, int style, Locale locale) {
if(icuTimeZone == null || !ID.equals(icuTimeZone.getID())){
icuTimeZone = com.ibm.icu.util.TimeZone.getTimeZone(ID); icuTimeZone = com.ibm.icu.util.TimeZone.getTimeZone(ID);
} }
return icuTimeZone.getDisplayName( return icuTimeZone.getDisplayName(daylightTime, style, locale);
daylightTime, style, locale);
} }
/**
* Gets the ID of this {@code TimeZone}.
*
* @return the time zone ID string.
*/
public String getID() { public String getID() {
return ID; return ID;
} }
/**
* Gets the daylight savings offset in milliseconds for this {@code TimeZone}.
* <p>
* This implementation returns 3600000 (1 hour), or 0 if the time zone does
* not observe daylight savings.
* <p>
* Subclasses may override to return daylight savings values other than 1
* hour.
* <p>
*
* @return the daylight savings offset in milliseconds if this {@code TimeZone}
* observes daylight savings, zero otherwise.
*/
public int getDSTSavings() { public int getDSTSavings() {
if (useDaylightTime()) { if (useDaylightTime()) {
return 3600000; return 3600000;
@ -210,15 +124,6 @@ public abstract class TimeZone implements TSerializable, TCloneable {
return 0; return 0;
} }
/**
* Gets the offset from GMT of this {@code TimeZone} for the specified date. The
* offset includes daylight savings time if the specified date is within the
* daylight savings time period.
*
* @param time
* the date in milliseconds since January 1, 1970 00:00:00 GMT
* @return the offset from GMT in milliseconds.
*/
public int getOffset(long time) { public int getOffset(long time) {
if (inDaylightTime(new Date(time))) { if (inDaylightTime(new Date(time))) {
return getRawOffset() + getDSTSavings(); return getRawOffset() + getDSTSavings();
@ -226,54 +131,16 @@ public abstract class TimeZone implements TSerializable, TCloneable {
return getRawOffset(); return getRawOffset();
} }
/** abstract public int getOffset(int era, int year, int month, int day, int dayOfWeek, int time);
* Gets the offset from GMT of this {@code TimeZone} for the specified date and
* time. The offset includes daylight savings time if the specified date and
* time are within the daylight savings time period.
*
* @param era
* the {@code GregorianCalendar} era, either {@code GregorianCalendar.BC} or
* {@code GregorianCalendar.AD}.
* @param year
* the year.
* @param month
* the {@code Calendar} month.
* @param day
* the day of the month.
* @param dayOfWeek
* the {@code Calendar} day of the week.
* @param time
* the time of day in milliseconds.
* @return the offset from GMT in milliseconds.
*/
abstract public int getOffset(int era, int year, int month, int day,
int dayOfWeek, int time);
/**
* Gets the offset for standard time from GMT for this {@code TimeZone}.
*
* @return the offset from GMT in milliseconds.
*/
abstract public int getRawOffset(); abstract public int getRawOffset();
/**
* Gets the {@code TimeZone} with the specified ID.
*
* @param name
* a time zone string ID.
* @return the {@code TimeZone} with the specified ID or null if no {@code TimeZone} with
* the specified ID exists.
*/
public static synchronized TimeZone getTimeZone(String name) { public static synchronized TimeZone getTimeZone(String name) {
if (AvailableZones == null) { if (AvailableZones == null) {
initializeAvailable(); initializeAvailable();
} }
TimeZone zone = AvailableZones.get(name); TimeZone zone = AvailableZones.get(name);
if(zone == null && isAvailableIDInICU(name)){
appendAvailableZones(name);
zone = AvailableZones.get(name);
}
if (zone == null) { if (zone == null) {
if (name.startsWith("GMT") && name.length() > 3) { if (name.startsWith("GMT") && name.length() > 3) {
char sign = name.charAt(3); char sign = name.charAt(3);
@ -287,10 +154,8 @@ public abstract class TimeZone implements TSerializable, TCloneable {
int index = position[0]; int index = position[0];
if (index != -1) { if (index != -1) {
int raw = hour * 3600000; int raw = hour * 3600000;
if (index < formattedName.length() if (index < formattedName.length() && formattedName.charAt(index) == ':') {
&& formattedName.charAt(index) == ':') { int minute = parseNumber(formattedName, index + 1, position);
int minute = parseNumber(formattedName, index + 1,
position);
if (position[0] == -1 || minute < 0 || minute > 59) { if (position[0] == -1 || minute < 0 || minute > 59) {
return (TimeZone) GMT.clone(); return (TimeZone) GMT.clone();
} }
@ -339,15 +204,6 @@ public abstract class TimeZone implements TSerializable, TCloneable {
return buf.toString(); return buf.toString();
} }
/**
* Returns whether the specified {@code TimeZone} has the same raw offset as this
* {@code TimeZone}.
*
* @param zone
* a {@code TimeZone}.
* @return {@code true} when the {@code TimeZone} have the same raw offset, {@code false}
* otherwise.
*/
public boolean hasSameRules(TimeZone zone) { public boolean hasSameRules(TimeZone zone) {
if (zone == null) { if (zone == null) {
return false; return false;
@ -355,21 +211,11 @@ public abstract class TimeZone implements TSerializable, TCloneable {
return getRawOffset() == zone.getRawOffset(); return getRawOffset() == zone.getRawOffset();
} }
/**
* Returns whether the specified {@code Date} is in the daylight savings time period for
* this {@code TimeZone}.
*
* @param time
* a {@code Date}.
* @return {@code true} when the {@code Date} is in the daylight savings time period, {@code false}
* otherwise.
*/
abstract public boolean inDaylightTime(Date time); abstract public boolean inDaylightTime(Date time);
private static int parseNumber(String string, int offset, int[] position) { private static int parseNumber(String string, int offset, int[] position) {
int index = offset, length = string.length(), digit, result = 0; int index = offset, length = string.length(), digit, result = 0;
while (index < length while (index < length && (digit = Character.digit(string.charAt(index), 10)) != -1) {
&& (digit = Character.digit(string.charAt(index), 10)) != -1) {
index++; index++;
result = result * 10 + digit; result = result * 10 + digit;
} }
@ -377,15 +223,6 @@ public abstract class TimeZone implements TSerializable, TCloneable {
return result; return result;
} }
/**
* Sets the default time zone. If passed {@code null}, then the next
* time {@link #getDefault} is called, the default time zone will be
* determined. This behavior is slightly different than the canonical
* description of this method, but it follows the spirit of it.
*
* @param timezone
* a {@code TimeZone} object.
*/
public static synchronized void setDefault(TimeZone timezone) { public static synchronized void setDefault(TimeZone timezone) {
if (timezone != null) { if (timezone != null) {
setICUDefaultTimeZone(timezone); setICUDefaultTimeZone(timezone);
@ -393,8 +230,7 @@ public abstract class TimeZone implements TSerializable, TCloneable {
return; return;
} }
String zone = AccessController.doPrivileged(new PriviAction<String>( String zone = AccessController.doPrivileged(new PriviAction<String>("user.timezone"));
"user.timezone"));
// sometimes DRLVM incorrectly adds "\n" to the end of timezone ID // sometimes DRLVM incorrectly adds "\n" to the end of timezone ID
if (zone != null && zone.contains("\n")) { if (zone != null && zone.contains("\n")) {
@ -413,15 +249,14 @@ public abstract class TimeZone implements TSerializable, TCloneable {
if (isCustomTimeZone[0]) { if (isCustomTimeZone[0]) {
// build a new SimpleTimeZone // build a new SimpleTimeZone
switch (tzinfo[1]) { switch (tzinfo[1]) {
case 0: case 0:
// does not observe DST // does not observe DST
Default = new SimpleTimeZone(tzinfo[0], zoneId); Default = new SimpleTimeZone(tzinfo[0], zoneId);
break; break;
default: default:
// observes DST // observes DST
Default = new SimpleTimeZone(tzinfo[0], zoneId, tzinfo[5], Default = new SimpleTimeZone(tzinfo[0], zoneId, tzinfo[5], tzinfo[4], tzinfo[3], tzinfo[2],
tzinfo[4], tzinfo[3], tzinfo[2], tzinfo[9], tzinfo[9], tzinfo[8], tzinfo[7], tzinfo[6], tzinfo[1]);
tzinfo[8], tzinfo[7], tzinfo[6], tzinfo[1]);
} }
} else { } else {
// get TimeZone // get TimeZone
@ -435,32 +270,23 @@ public abstract class TimeZone implements TSerializable, TCloneable {
} }
private static void setICUDefaultTimeZone(TimeZone timezone) { private static void setICUDefaultTimeZone(TimeZone timezone) {
final com.ibm.icu.util.TimeZone icuTZ = com.ibm.icu.util.TimeZone final com.ibm.icu.util.TimeZone icuTZ = com.ibm.icu.util.TimeZone.getTimeZone(timezone.getID());
.getTimeZone(timezone.getID());
AccessController AccessController.doPrivileged(new PrivilegedAction<java.lang.reflect.Field>() {
.doPrivileged(new PrivilegedAction<java.lang.reflect.Field>() { public java.lang.reflect.Field run() {
public java.lang.reflect.Field run() { java.lang.reflect.Field field = null;
java.lang.reflect.Field field = null; try {
try { field = com.ibm.icu.util.TimeZone.class.getDeclaredField("defaultZone");
field = com.ibm.icu.util.TimeZone.class field.setAccessible(true);
.getDeclaredField("defaultZone"); field.set("defaultZone", icuTZ);
field.setAccessible(true); } catch (Exception e) {
field.set("defaultZone", icuTZ); return null;
} catch (Exception e) { }
return null; return field;
} }
return field; });
}
});
} }
/**
* Sets the ID of this {@code TimeZone}.
*
* @param name
* a string which is the time zone ID.
*/
public void setID(String name) { public void setID(String name) {
if (name == null) { if (name == null) {
throw new NullPointerException(); throw new NullPointerException();
@ -468,42 +294,9 @@ public abstract class TimeZone implements TSerializable, TCloneable {
ID = name; ID = name;
} }
/**
* Sets the offset for standard time from GMT for this {@code TimeZone}.
*
* @param offset
* the offset from GMT in milliseconds.
*/
abstract public void setRawOffset(int offset); abstract public void setRawOffset(int offset);
/**
* Returns whether this {@code TimeZone} has a daylight savings time period.
*
* @return {@code true} if this {@code TimeZone} has a daylight savings time period, {@code false}
* otherwise.
*/
abstract public boolean useDaylightTime(); abstract public boolean useDaylightTime();
/** private static native String getCustomTimeZone(int[] tzinfo, boolean[] isCustomTimeZone);
* Gets the name and the details of the user-selected TimeZone on the
* device.
*
* @param tzinfo
* int array of 10 elements to be filled with the TimeZone
* information. Once filled, the contents of the array are
* formatted as follows: tzinfo[0] -> the timezone offset;
* tzinfo[1] -> the dst adjustment; tzinfo[2] -> the dst start
* hour; tzinfo[3] -> the dst start day of week; tzinfo[4] -> the
* dst start week of month; tzinfo[5] -> the dst start month;
* tzinfo[6] -> the dst end hour; tzinfo[7] -> the dst end day of
* week; tzinfo[8] -> the dst end week of month; tzinfo[9] -> the
* dst end month;
* @param isCustomTimeZone
* boolean array of size 1 that indicates if a timezone match is
* found
* @return the name of the TimeZone or null if error occurs in native
* method.
*/
private static native String getCustomTimeZone(int[] tzinfo,
boolean[] isCustomTimeZone);
} }