/* /os/dynamap/java/COM/odi/util/OSDate.java */ //package COM.odi.util; import COM.odi.ClassInfo; import COM.odi.Field; import COM.odi.Persistent; import COM.odi.ObjectStore; import COM.odi.GenericObject; import COM.odi.BadArrayDimensionsException; import java.util.Date; //As a convenience, Object Design makes available a few third-party //persistence-capable versions of standard Java classes. //Caution //Object Design does not provide support for these classes and does not //guarantee that these classes work. While Object Design does look over //these classes before making them available, Object Design is not obligated //to fix them if any problems occur. /** * OSDate implements a persistence-capable class with the * same constructors and methods as java.util.Date.

* * @see java.util.Date */ public class OSDate implements java.io.Serializable { /* This is a persistent instance variable that represents the value of the date when the object is in the database (and not in Java). */ private long persistentDate; /* This is a transient instance variable that holds a java.util.Date object, that represents the value of the date when the object is in Java. */ private transient Date transientDate; /* After reading an OSDate from the database, create a new Date object from the persistent long value, and store it in transientDate. */ public void postInitializeContents() { transientDate = new Date(persistentDate); } /* Before storing an OSDate into the database, update the persistent long value from the current value of the transientDate. */ public void preFlushContents() { persistentDate = transientDate.getTime(); } /* Before clearing the contents of an OSDate object, clear the transientDate as well. */ public void preClearContents() { transientDate = null; } /* After Object Serialization reads in the contents of an OSDate object, create a new Date object from the persistent long value, and store it in transientDate. */ private void readObject(java.io.ObjectInputStream stream) throws ClassNotFoundException, java.io.IOException { stream.defaultReadObject(); transientDate = new Date(persistentDate); } /* The constructors and methods corresponding to those of the java.util.Date class. */ /** * Creates today's date/time. */ public OSDate () { transientDate = new Date(); } /** * Creates a date. * The fields are normalized before the Date object is created. * The argument does not have to be in the correct range. For * example, the 32nd of January is correctly interpreted as the * 1st of February. You can use this to figure out what day a * particular date falls on. * @param date the value of the argument to be created */ public OSDate (long date) { transientDate = new Date(date); } /** * Creates a date. * The fields are normalized before the Date object is created. * The arguments do not have to be in the correct range. For example, * the 32nd of January is correctly interpreted as the 1st of February. * You can use this to figure out what day a particular date falls on. * @param year a year after 1900 * @param month a month between 0-11 * @param date day of the month between 1-31 */ public OSDate (int year, int month, int date) { transientDate = new Date(year, month, date, 0, 0, 0); } /** * Creates a date. * The fields are normalized before the Date object is created. * The arguments do not have to be in the correct range. For example, * the 32nd of January is correctly interpreted as the 1st of February. * You can use this to figure out what day a particular date falls on. * @param year a year after 1900 * @param month a month between 0-11 * @param date day of the month between 1-31 * @param hrs hours between 0-23 * @param min minutes between 0-59 */ public OSDate (int year, int month, int date, int hrs, int min) { transientDate = new Date(year, month, date, hrs, min, 0); } /** * Creates a date. The fields are normalized before the Date object is * created. The arguments do not have to be in the correct range. For * example, the 32nd of January is correctly interpreted as the 1st of * February. You can use this to figure out what day a particular date * falls on. * @param year a year after 1900 * @param month a month between 0-11 * @param date day of the month between 1-31 * @param hrs hours between 0-23 * @param min minutes between 0-59 * @param sec seconds between 0-59 */ public OSDate (int year, int month, int date, int hrs, int min, int sec) { transientDate = new Date(year, month, date, hrs, min, sec); } /** * Creates a date from a string according to the syntax * accepted by parse(). */ public OSDate (String s) { transientDate = new Date(s); } /** * Calculates a UTC value from YMDHMS. Interpretes * the parameters in UTC, not in the local time zone. * @param year a year after 1900 * @param month a month between 0-11 * @param date day of the month between 1-31 * @param hrs hours between 0-23 * @param min minutes between 0-59 * @param sec seconds between 0-59 */ public static long UTC(int year, int month, int date, int hrs, int min, int sec) { return Date.UTC(year, month, date, hrs, min, sec); } /** * Given a string representing a time, parse it and return the time value. * It accepts many syntaxes, but most importantly, in accepts the IETF * standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It understands * the continental US time zone abbreviations, but for general use, a * timezone offset should be used: "Sat, 12 Aug 1995 13:30:00 GMT+0430" * (4 hours, 30 minutes west of the Greenwich meridian). * If no time zone is specified, the local time zone is assumed. * GMT and UTC are considered equivalent. */ public static long parse(String s) { return Date.parse(s); } /** * Returns the year after 1900. */ public int getYear() { return transientDate.getYear(); } /** * Sets the year. * @param year the year value */ public void setYear(int year) { transientDate.setYear(year); } /** * Returns the month. This method assigns months with the * values 0-11, with January beginning at value 0. */ public int getMonth() { return transientDate.getMonth(); } /** * Sets the month. * @param month the month value (0-11) */ public void setMonth(int month) { transientDate.setMonth(month); } /** * Returns the day of the month. This method assigns days * with the values of 1 to 31. */ public int getDate() { return transientDate.getDate(); } /** * Sets the date. * @param date the day value */ public void setDate(int date) { transientDate.setDate(date); } /** * Returns the day of the week. This method assigns days * of the week with the values 0-6, with 0 being Sunday. */ public int getDay() { return transientDate.getDay(); } /** * Returns the hour. This method assigns the value of the * hours of the day to range from 0 to 23, with midnight equal * to 0. */ public int getHours() { return transientDate.getHours(); } /** * Sets the hours. * @param hours the hour value */ public void setHours(int hours) { transientDate.setHours(hours); } /** * Returns the minute. This method assigns the minutes of an * hour to be any value from 0 to 59. */ public int getMinutes() { return transientDate.getMinutes(); } /** * Sets the minutes. * @param minutes the value of the minutes */ public void setMinutes(int minutes) { transientDate.setMinutes(minutes); } /** * Returns the second. This method assigns the seconds of * a minute to values of 0-59. */ public int getSeconds() { return transientDate.getSeconds(); } /** * Sets the seconds. * @param seconds the second value */ public void setSeconds(int seconds) { transientDate.setSeconds(seconds); } /** * Returns the time in milliseconds since the epoch. */ public long getTime() { return transientDate.getTime(); } /** * Sets the time. * @param time The new time value in milliseconds since the epoch. */ public void setTime(long time) { transientDate.setTime(time); } /** * Checks whether this date comes before the specified date. * @param when the date to compare * @return true if the original date comes before the specified * one; false otherwise. */ public boolean before(Date when) { return transientDate.before(when); } /** * Checks whether this date comes after the specified date. * @param when the date to compare * @return true if the original date comes after the specified * one; false otherwise. */ public boolean after(Date when) { return transientDate.after(when); } /** * Compares this object against the specified object. * @param obj the object to compare with * @return true if the objects are the same; false otherwise. */ public boolean equals(Object obj) { return obj != null && obj instanceof OSDate && getTime() == ((OSDate) obj).getTime(); } /** * Computes a hashCode. */ public int hashCode() { return transientDate.hashCode(); } /** * Converts a date to a String, using the UNIX ctime conventions. */ public String toString() { return transientDate.toString(); } /** * Converts a date to a String, using the locale conventions. */ public String toLocaleString() { return transientDate.toLocaleString(); } /** * Converts a date to a String, using the Internet GMT conventions. */ public String toGMTString() { return transientDate.toGMTString(); } /** * Return the time zone offset in minutes for the current locale that is appropriate * for this time. This value would be a constant except for * daylight savings time. */ public int getTimezoneOffset() { return transientDate.getTimezoneOffset(); } }