/* java.util.Date Copyright (C) 1998, 1999, 2000, 2001, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package java.util; /** *

* This class represents a specific time in milliseconds since the epoch. * The epoch is 1970, January 1 00:00:00.0000 UTC. *

*

* Date is intended to reflect universal time coordinate (UTC), * but this depends on the underlying host environment. Most operating systems * don't handle the leap second, which occurs about once every year or * so. The leap second is added to the last minute of the day on either * the 30th of June or the 31st of December, creating a minute 61 seconds * in length. *

*

* The representations of the date fields are as follows: *

*

*

* Prior to JDK 1.1, this class was the sole class handling date and time * related functionality. However, this particular solution was not * amenable to internationalization. The new Calendar * class should now be used to handle dates and times, with Date * being used only for values in milliseconds since the epoch. The * Calendar class, and its concrete implementations, handle * the interpretation of these values into minutes, hours, days, months * and years. The formatting and parsing of dates is left to the * DateFormat class, which is able to handle the different * types of date format which occur in different locales. *

* * @see Calendar * @see GregorianCalendar * @see java.text.DateFormat * @author Jochen Hoenicke * @author Per Bothner (bothner@cygnus.com) * @author Andrew John Hughes (gnu_andrew@member.fsf.org) */ public class Date { /** * The time in milliseconds since the epoch. */ private transient long time; /** * An array of week names used to map names to integer values. */ private static final String[] weekNames = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; /** * An array of month names used to map names to integer values. */ private static final String[] monthNames = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; /** * Creates a new Date Object representing the current time. */ public Date() { time = System.currentTimeMillis(); } /** * Creates a new Date Object representing the given time. * * @param time the time in milliseconds since the epoch. */ public Date(long time) { this.time = time; } /** * Gets the time represented by this object. * * @return the time in milliseconds since the epoch. */ public long getTime() { return time; } /** * Sets the time which this object should represent. * * @param time the time in milliseconds since the epoch. */ public void setTime(long time) { this.time = time; } /** * Compares two dates for equality. * * @param obj the object to compare. * @return true, if obj is a Date object and the time represented * by obj is exactly the same as the time represented by this * object. */ public boolean equals(Object obj) { return (obj instanceof Date && time == ((Date) obj).time); } /** * Computes the hash code of this Date as the * XOR of the most significant and the least significant * 32 bits of the 64 bit milliseconds value. * * @return the hash code. */ public int hashCode() { return (int) time ^ (int) (time >>> 32); } /** *

* Returns a string representation of this date using * the following date format: *

*

* day mon dd hh:mm:ss zz yyyy *

*

where the fields used here are: *

*

* The DateFormat class should now be * preferred over using this method. *

* * @return A string of the form 'day mon dd hh:mm:ss zz yyyy' */ public String toString() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); String day = "0" + cal.get(Calendar.DATE); String hour = "0" + cal.get(Calendar.HOUR_OF_DAY); String min = "0" + cal.get(Calendar.MINUTE); String sec = "0" + cal.get(Calendar.SECOND); String year = "000" + cal.get(Calendar.YEAR); return weekNames[cal.get(Calendar.DAY_OF_WEEK) - 1] + " " + monthNames[cal.get(Calendar.MONTH)] + " " + day.substring(day.length() - 2) + " " + hour.substring(hour.length() - 2) + ":" + min.substring(min.length() - 2) + ":" + sec.substring(sec.length() - 2) + " " + cal.getTimeZone().getID() + " " + year.substring(year.length() - 4); } }