1 *java.util.Date* *Date* The class Date represents a specific instant
5 extends |java.lang.Object|
6 implements |java.io.Serializable|
10 |java.util.Date_Description|
11 |java.util.Date_Fields|
12 |java.util.Date_Constructors|
13 |java.util.Date_Methods|
15 ================================================================================
17 *java.util.Date_Constructors*
18 |java.util.Date()|Allocates a Date object and initializes it so that it repre
19 |java.util.Date(int,int,int)|Allocates a Date object and initializes it so that
20 |java.util.Date(int,int,int,int,int)|Allocates a Date object and initializes it
21 |java.util.Date(int,int,int,int,int,int)|Allocates a Date object and initialize
22 |java.util.Date(long)|Allocates a Date object and initializes it to represent
23 |java.util.Date(String)|Allocates a Date object and initializes it so that it
25 *java.util.Date_Methods*
26 |java.util.Date.after(Date)|Tests if this date is after the specified date.
27 |java.util.Date.before(Date)|Tests if this date is before the specified date.
28 |java.util.Date.clone()|Return a copy of this object.
29 |java.util.Date.compareTo(Date)|Compares two Dates for ordering.
30 |java.util.Date.equals(Object)|Compares two dates for equality.
31 |java.util.Date.getDate()|Returns the day of the month represented by this Date
32 |java.util.Date.getDay()|Returns the day of the week represented by this date.
33 |java.util.Date.getHours()|Returns the hour represented by this Date object.
34 |java.util.Date.getMinutes()|Returns the number of minutes past the hour repres
35 |java.util.Date.getMonth()|Returns a number representing the month that contain
36 |java.util.Date.getSeconds()|Returns the number of seconds past the minute repr
37 |java.util.Date.getTime()|Returns the number of milliseconds since January 1, 1
38 |java.util.Date.getTimezoneOffset()|Returns the offset, measured in minutes, fo
39 |java.util.Date.getYear()|Returns a value that is the result of subtracting 190
40 |java.util.Date.hashCode()|Returns a hash code value for this object.
41 |java.util.Date.parse(String)|Attempts to interpret the string s as a represent
42 |java.util.Date.setDate(int)|Sets the day of the month of this Date object to t
43 |java.util.Date.setHours(int)|Sets the hour of this Date object to the specifie
44 |java.util.Date.setMinutes(int)|Sets the minutes of this Date object to the spe
45 |java.util.Date.setMonth(int)|Sets the month of this date to the specified valu
46 |java.util.Date.setSeconds(int)|Sets the seconds of this Date to the specified
47 |java.util.Date.setTime(long)|Sets this Date object to represent a point in tim
48 |java.util.Date.setYear(int)|Sets the year of this Date object to be the specif
49 |java.util.Date.toGMTString()|Creates a string representation of this Date obje
50 |java.util.Date.toLocaleString()|Creates a string representation of this Date o
51 |java.util.Date.toString()|Converts this Date object to a String of the form:
52 |java.util.Date.UTC(int,int,int,int,int,int)|Determines the date and time based
54 *java.util.Date_Description*
56 The class Date represents a specific instant in time, with millisecond
59 Prior to JDK1.1, the class Date had two additional functions. It allowed the
60 interpretation of dates as year, month, day, hour, minute, and second values.
61 It also allowed the formatting and parsing of date strings. Unfortunately, the
62 API for these functions was not amenable to internationalization. As of JDK1.1,
63 the Calendar class should be used to convert between dates and time fields and
64 the DateFormat class should be used to format and parse date strings. The
65 corresponding methods in Date are deprecated.
67 Although the Date class is intended to reflect coordinated universal time
68 (UTC), it may not do so exactly, depending on the host environment of the Java
69 Virtual Machine. Nearly all modern operating systems assume that 1day= 246060=
70 86400 seconds in all cases. In UTC, however, about once every year or two there
71 is an extra second, called a "leap second." The leap second is always added as
72 the last second of the day, and always on December 31 or June 30. For example,
73 the last minute of the year 1995 was 61 seconds long, thanks to an added leap
74 second. Most computer clocks are not accurate enough to be able to reflect the
75 leap-second distinction.
77 Some computer standards are defined in terms of Greenwich mean time (GMT),
78 which is equivalent to universal time (UT). GMT is the "civil" name for the
79 standard; UT is the "scientific" name for the same standard. The distinction
80 between UTC and UT is that UTC is based on an atomic clock and UT is based on
81 astronomical observations, which for all practical purposes is an invisibly
82 fine hair to split. Because the earth's rotation is not uniform (it slows down
83 and speeds up in complicated ways), UT does not always flow uniformly. Leap
84 seconds are introduced as needed into UTC so as to keep UTC within 0.9 seconds
85 of UT1, which is a version of UT with certain corrections applied. There are
86 other time and date systems as well; for example, the time scale used by the
87 satellite-based global positioning system (GPS) is synchronized to UTC but is
88 not adjusted for leap seconds. An interesting source of further information is
89 the U.S. Naval Observatory, particularly the Directorate of Time at:
91 http://tycho.usno.navy.mil
93 and their definitions of "Systems of Time" at:
95 http://tycho.usno.navy.mil/systime.html
97 In all methods of class Date that accept or return year, month, date, hours,
98 minutes, and seconds values, the following representations are used:
100 A year y is represented by the integer y-1900. A month is represented by an
101 integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is
102 December. A date (day of month) is represented by an integer from 1 to 31 in
103 the usual manner. An hour is represented by an integer from 0 to 23. Thus, the
104 hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is
105 hour 12. A minute is represented by an integer from 0 to 59 in the usual
106 manner. A second is represented by an integer from 0 to 61; the values 60 and
107 61 occur only for leap seconds and even then only in Java implementations that
108 actually track leap seconds correctly. Because of the manner in which leap
109 seconds are currently introduced, it is extremely unlikely that two leap
110 seconds will occur in the same minute, but this specification follows the date
111 and time conventions for ISO C.
113 In all cases, arguments given to methods for these purposes need not fall
114 within the indicated ranges; for example, a date may be specified as January 32
115 and is interpreted as meaning February 1.
122 Allocates a Date object and initializes it so that it represents the time at
123 which it was allocated, measured to the nearest millisecond.
126 *java.util.Date(int,int,int)*
133 Allocates a Date object and initializes it so that it represents midnight,
134 local time, at the beginning of the day specified by the year, month, and date
137 year - the year minus 1900.
138 month - the month between 0-11.
139 date - the day of the month between 1-31.
141 *java.util.Date(int,int,int,int,int)*
150 Allocates a Date object and initializes it so that it represents the instant at
151 the start of the minute specified by the year, month, date, hrs, and min
152 arguments, in the local time zone.
154 year - the year minus 1900.
155 month - the month between 0-11.
156 date - the day of the month between 1-31.
157 hrs - the hours between 0-23.
158 min - the minutes between 0-59.
160 *java.util.Date(int,int,int,int,int,int)*
170 Allocates a Date object and initializes it so that it represents the instant at
171 the start of the second specified by the year, month, date, hrs, min, and sec
172 arguments, in the local time zone.
174 year - the year minus 1900.
175 month - the month between 0-11.
176 date - the day of the month between 1-31.
177 hrs - the hours between 0-23.
178 min - the minutes between 0-59.
179 sec - the seconds between 0-59.
181 *java.util.Date(long)*
183 public Date(long date)
185 Allocates a Date object and initializes it to represent the specified number of
186 milliseconds since the standard base time known as "the epoch", namely January
187 1, 1970, 00:00:00 GMT.
189 date - the milliseconds since January 1, 1970, 00:00:00 GMT.
191 *java.util.Date(String)*
193 public Date(java.lang.String s)
195 Allocates a Date object and initializes it so that it represents the date and
196 time indicated by the string s, which is interpreted as if by the
197 (|java.util.Date|) method.
199 s - a string representation of the date.
201 *java.util.Date.after(Date)*
203 public boolean after(java.util.Date when)
205 Tests if this date is after the specified date.
209 Returns: true if and only if the instant represented by this Date object is strictly
210 later than the instant represented by when; false otherwise.
211 *java.util.Date.before(Date)*
213 public boolean before(java.util.Date when)
215 Tests if this date is before the specified date.
219 Returns: true if and only if the instant of time represented by this Date object is
220 strictly earlier than the instant represented by when; false
222 *java.util.Date.clone()*
224 public |java.lang.Object| clone()
226 Return a copy of this object.
229 *java.util.Date.compareTo(Date)*
231 public int compareTo(java.util.Date anotherDate)
233 Compares two Dates for ordering.
235 anotherDate - the Date to be compared.
237 Returns: the value 0 if the argument Date is equal to this Date; a value less than 0 if
238 this Date is before the Date argument; and a value greater than 0
239 if this Date is after the Date argument.
240 *java.util.Date.equals(Object)*
242 public boolean equals(java.lang.Object obj)
244 Compares two dates for equality. The result is true if and only if the argument
245 is not null and is a Date object that represents the same point in time, to the
246 millisecond, as this object.
248 Thus, two Date objects are equal if and only if the getTime method returns the
249 same long value for both.
251 obj - the object to compare with.
253 Returns: true if the objects are the same; false otherwise.
254 *java.util.Date.getDate()*
258 Returns the day of the month represented by this Date object. The value
259 returned is between 1 and 31 representing the day of the month that contains or
260 begins with the instant in time represented by this Date object, as interpreted
261 in the local time zone.
264 Returns: the day of the month represented by this date.
265 *java.util.Date.getDay()*
269 Returns the day of the week represented by this date. The returned value (0 =
270 Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 =
271 Saturday) represents the day of the week that contains or begins with the
272 instant in time represented by this Date object, as interpreted in the local
276 Returns: the day of the week represented by this date.
277 *java.util.Date.getHours()*
279 public int getHours()
281 Returns the hour represented by this Date object. The returned value is a
282 number (0 through 23) representing the hour within the day that contains or
283 begins with the instant in time represented by this Date object, as interpreted
284 in the local time zone.
287 Returns: the hour represented by this date.
288 *java.util.Date.getMinutes()*
290 public int getMinutes()
292 Returns the number of minutes past the hour represented by this date, as
293 interpreted in the local time zone. The value returned is between 0 and 59.
296 Returns: the number of minutes past the hour represented by this date.
297 *java.util.Date.getMonth()*
299 public int getMonth()
301 Returns a number representing the month that contains or begins with the
302 instant in time represented by this Date object. The value returned is between
303 0 and 11, with the value 0 representing January.
306 Returns: the month represented by this date.
307 *java.util.Date.getSeconds()*
309 public int getSeconds()
311 Returns the number of seconds past the minute represented by this date. The
312 value returned is between 0 and 61. The values 60 and 61 can only occur on
313 those Java Virtual Machines that take leap seconds into account.
316 Returns: the number of seconds past the minute represented by this date.
317 *java.util.Date.getTime()*
319 public long getTime()
321 Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
322 represented by this Date object.
325 Returns: the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by
327 *java.util.Date.getTimezoneOffset()*
329 public int getTimezoneOffset()
331 Returns the offset, measured in minutes, for the local time zone relative to
332 UTC that is appropriate for the time represented by this Date object.
334 For example, in Massachusetts, five time zones west of Greenwich:
336 new Date(96, 1, 14).getTimezoneOffset() returns 300
338 because on February 14, 1996, standard time (Eastern Standard Time) is in use,
339 which is offset five hours from UTC; but:
341 new Date(96, 5, 1).getTimezoneOffset() returns 240
343 because on June 1, 1996, daylight saving time (Eastern Daylight Time) is in
344 use, which is offset only four hours from UTC. This method produces the same
345 result as if it computed:
347 (this.getTime() - UTC(this.getYear(), this.getMonth(), this.getDate(),
348 this.getHours(), this.getMinutes(), this.getSeconds())) / (60 * 1000)
351 Returns: the time-zone offset, in minutes, for the current time zone.
352 *java.util.Date.getYear()*
356 Returns a value that is the result of subtracting 1900 from the year that
357 contains or begins with the instant in time represented by this Date object, as
358 interpreted in the local time zone.
361 Returns: the year represented by this date, minus 1900.
362 *java.util.Date.hashCode()*
364 public int hashCode()
366 Returns a hash code value for this object. The result is the exclusive OR of
367 the two halves of the primitive long value returned by the (|java.util.Date|)
368 method. That is, the hash code is the value of the expression:
370 (int)(this.getTime()^(this.getTime() >>> 32))
373 Returns: a hash code value for this object.
374 *java.util.Date.parse(String)*
376 public static long parse(java.lang.String s)
378 Attempts to interpret the string s as a representation of a date and time. If
379 the attempt is successful, the time indicated is returned represented as the
380 distance, measured in milliseconds, of that time from the epoch (00:00:00 GMT
381 on January 1, 1970). If the attempt fails, an IllegalArgumentException is
384 It accepts many syntaxes; in particular, it recognizes the IETF standard date
385 syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also understands the continental
386 U.S. time-zone abbreviations, but for general use, a time-zone offset should be
387 used: "Sat, 12 Aug 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the
388 Greenwich meridian). If no time zone is specified, the local time zone is
389 assumed. GMT and UTC are considered equivalent.
391 The string s is processed from left to right, looking for data of interest. Any
392 material in s that is within the ASCII parenthesis characters ( and ) is
393 ignored. Parentheses may be nested. Otherwise, the only characters permitted
394 within s are these ASCII characters:
396 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789,+-:/
398 and whitespace characters. A consecutive sequence of decimal digits is treated
399 as a decimal number: If a number is preceded by + or - and a year has already
400 been recognized, then the number is a time-zone offset. If the number is less
401 than 24, it is an offset measured in hours. Otherwise, it is regarded as an
402 offset in minutes, expressed in 24-hour time format without punctuation. A
403 preceding - means a westward offset. Time zone offsets are always relative to
404 UTC (Greenwich). Thus, for example, -5 occurring in the string would mean "five
405 hours west of Greenwich" and +0430 would mean "four hours and thirty minutes
406 east of Greenwich." It is permitted for the string to specify GMT, UT, or UTC
407 redundantly-for example, GMT-5 or utc+0430. The number is regarded as a year
408 number if one of the following conditions is true:
410 The number is equal to or greater than 70 and followed by a space, comma,
411 slash, or end of string The number is less than 70, and both a month and a day
412 of the month have already been recognized
414 If the recognized year number is less than 100, it is interpreted as an
415 abbreviated year relative to a century of which dates are within 80 years
416 before and 19 years after the time when the Date class is initialized. After
417 adjusting the year number, 1900 is subtracted from it. For example, if the
418 current year is 1999 then years in the range 19 to 99 are assumed to mean 1919
419 to 1999, while years from 0 to 18 are assumed to mean 2000 to 2018. Note that
420 this is slightly different from the interpretation of years less than 100 that
421 is used in (|java.text.SimpleDateFormat|) . If the number is followed by a
422 colon, it is regarded as an hour, unless an hour has already been recognized,
423 in which case it is regarded as a minute. If the number is followed by a slash,
424 it is regarded as a month (it is decreased by 1 to produce a number in the
425 range 0 to 11), unless a month has already been recognized, in which case it is
426 regarded as a day of the month. If the number is followed by whitespace, a
427 comma, a hyphen, or end of string, then if an hour has been recognized but not
428 a minute, it is regarded as a minute; otherwise, if a minute has been
429 recognized but not a second, it is regarded as a second; otherwise, it is
430 regarded as a day of the month. A consecutive sequence of letters is regarded
431 as a word and treated as follows: A word that matches AM, ignoring case, is
432 ignored (but the parse fails if an hour has not been recognized or is less than
433 1 or greater than 12). A word that matches PM, ignoring case, adds 12 to the
434 hour (but the parse fails if an hour has not been recognized or is less than 1
435 or greater than 12). Any word that matches any prefix of SUNDAY, MONDAY,
436 TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, or SATURDAY, ignoring case, is ignored.
437 For example, sat, Friday, TUE, and Thurs are ignored. Otherwise, any word that
438 matches any prefix of JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
439 SEPTEMBER, OCTOBER, NOVEMBER, or DECEMBER, ignoring case, and considering them
440 in the order given here, is recognized as specifying a month and is converted
441 to a number (0 to 11). For example, aug, Sept, april, and NOV are recognized as
442 months. So is Ma, which is recognized as MARCH, not MAY. Any word that matches
443 GMT, UT, or UTC, ignoring case, is treated as referring to UTC. Any word that
444 matches EST, CST, MST, or PST, ignoring case, is recognized as referring to the
445 time zone in North America that is five, six, seven, or eight hours west of
446 Greenwich, respectively. Any word that matches EDT, CDT, MDT, or PDT, ignoring
447 case, is recognized as referring to the same time zone, respectively, during
448 daylight saving time. Once the entire string s has been scanned, it is
449 converted to a time result in one of two ways. If a time zone or time-zone
450 offset has been recognized, then the year, month, day of month, hour, minute,
451 and second are interpreted in UTC and then the time-zone offset is applied.
452 Otherwise, the year, month, day of month, hour, minute, and second are
453 interpreted in the local time zone.
455 s - a string to be parsed as a date.
457 Returns: the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by
459 *java.util.Date.setDate(int)*
461 public void setDate(int date)
463 Sets the day of the month of this Date object to the specified value. This Date
464 object is modified so that it represents a point in time within the specified
465 day of the month, with the year, month, hour, minute, and second the same as
466 before, as interpreted in the local time zone. If the date was April 30, for
467 example, and the date is set to 31, then it will be treated as if it were on
468 May 1, because April has only 30 days.
470 date - the day of the month value between 1-31.
472 *java.util.Date.setHours(int)*
474 public void setHours(int hours)
476 Sets the hour of this Date object to the specified value. This Date object is
477 modified so that it represents a point in time within the specified hour of the
478 day, with the year, month, date, minute, and second the same as before, as
479 interpreted in the local time zone.
481 hours - the hour value.
483 *java.util.Date.setMinutes(int)*
485 public void setMinutes(int minutes)
487 Sets the minutes of this Date object to the specified value. This Date object
488 is modified so that it represents a point in time within the specified minute
489 of the hour, with the year, month, date, hour, and second the same as before,
490 as interpreted in the local time zone.
492 minutes - the value of the minutes.
494 *java.util.Date.setMonth(int)*
496 public void setMonth(int month)
498 Sets the month of this date to the specified value. This Date object is
499 modified so that it represents a point in time within the specified month, with
500 the year, date, hour, minute, and second the same as before, as interpreted in
501 the local time zone. If the date was October 31, for example, and the month is
502 set to June, then the new date will be treated as if it were on July 1, because
503 June has only 30 days.
505 month - the month value between 0-11.
507 *java.util.Date.setSeconds(int)*
509 public void setSeconds(int seconds)
511 Sets the seconds of this Date to the specified value. This Date object is
512 modified so that it represents a point in time within the specified second of
513 the minute, with the year, month, date, hour, and minute the same as before, as
514 interpreted in the local time zone.
516 seconds - the seconds value.
518 *java.util.Date.setTime(long)*
520 public void setTime(long time)
522 Sets this Date object to represent a point in time that is time milliseconds
523 after January 1, 1970 00:00:00 GMT.
525 time - the number of milliseconds.
527 *java.util.Date.setYear(int)*
529 public void setYear(int year)
531 Sets the year of this Date object to be the specified value plus 1900. This
532 Date object is modified so that it represents a point in time within the
533 specified year, with the month, date, hour, minute, and second the same as
534 before, as interpreted in the local time zone. (Of course, if the date was
535 February 29, for example, and the year is set to a non-leap year, then the new
536 date will be treated as if it were on March 1.)
538 year - the year value.
540 *java.util.Date.toGMTString()*
542 public |java.lang.String| toGMTString()
544 Creates a string representation of this Date object of the form:
546 where: d is the day of the month (1 through 31), as one or two decimal digits.
547 mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
548 yyyy is the year, as four decimal digits. hh is the hour of the day (00 through
549 23), as two decimal digits. mm is the minute within the hour (00 through 59),
550 as two decimal digits. ss is the second within the minute (00 through 61), as
551 two decimal digits. GMT is exactly the ASCII letters "GMT" to indicate
554 The result does not depend on the local time zone.
557 Returns: a string representation of this date, using the Internet GMT conventions.
558 *java.util.Date.toLocaleString()*
560 public |java.lang.String| toLocaleString()
562 Creates a string representation of this Date object in an
563 implementation-dependent form. The intent is that the form should be familiar
564 to the user of the Java application, wherever it may happen to be running. The
565 intent is comparable to that of the "%c" format supported by the strftime()
569 Returns: a string representation of this date, using the locale conventions.
570 *java.util.Date.toString()*
572 public |java.lang.String| toString()
574 Converts this Date object to a String of the form:
576 dow mon dd hh:mm:ss zzz yyyy
578 where: dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat). mon is
579 the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec). dd is
580 the day of the month (01 through 31), as two decimal digits. hh is the hour of
581 the day (00 through 23), as two decimal digits. mm is the minute within the
582 hour (00 through 59), as two decimal digits. ss is the second within the minute
583 (00 through 61, as two decimal digits. zzz is the time zone (and may reflect
584 daylight saving time). Standard time zone abbreviations include those
585 recognized by the method parse. If time zone information is not available, then
586 zzz is empty - that is, it consists of no characters at all. yyyy is the year,
587 as four decimal digits.
590 Returns: a string representation of this date.
591 *java.util.Date.UTC(int,int,int,int,int,int)*
593 public static long UTC(
601 Determines the date and time based on the arguments. The arguments are
602 interpreted as a year, month, day of the month, hour of the day, minute within
603 the hour, and second within the minute, exactly as for the Date constructor
604 with six arguments, except that the arguments are interpreted relative to UTC
605 rather than to the local time zone. The time indicated is returned represented
606 as the distance, measured in milliseconds, of that time from the epoch
607 (00:00:00 GMT on January 1, 1970).
609 year - the year minus 1900.
610 month - the month between 0-11.
611 date - the day of the month between 1-31.
612 hrs - the hours between 0-23.
613 min - the minutes between 0-59.
614 sec - the seconds between 0-59.
616 Returns: the number of milliseconds since January 1, 1970, 00:00:00 GMT for the date and
617 time specified by the arguments.