Java Basic Series--date class

Source: Internet
Author: User
Tags date now dateformat deprecated

Original works, can be reproduced, but please mark the source address: http://www.cnblogs.com/V1haoge/p/7126930.html

1. Overview of the Date class

The date class is the old class that began to exist from the JDK1.1, which provides a number of ways to manipulate the date, but it has been criticized, different starting numbers, the low internationalization of support, the JDK is also aware of this problem, the background proposed to use the Calendar class for date operations, date format to DateFormat, While we are no longer using most of the methods in the date class, there is still a part of the reserved content that refers to our talk.

2. Constructors

There are 6 constructors before the date class, of which four are already marked deprecated, and we are not looking at him, and we focus on the other two:

1     /**2 * Allocates a <code>Date</code> object and initializes it so3 * It represents the time at which it is allocated, measured to the4 * Nearest millisecond.5      *6      * @seeJava.lang.system#currenttimemillis ()7      */8      PublicDate () {9          This(System.currenttimemillis ());Ten     } One  A     /** - * Allocates a <code>Date</code> object and initializes it to - * represent the specified number of milliseconds since the the * Standard base time known as "the Epoch", namely January 1, - * 1970, 00:00:00 GMT. -      * -      * @paramdate the milliseconds since January 1, 1970, 00:00:00 GMT. +      * @seeJava.lang.system#currenttimemillis () -      */ +      PublicDate (Longdate) { AFasttime =date; at}

The first constructor is the parameterless constructor that obtains the current timestamp by calling the system's Currenttimemillis () method, which is a millisecond-level data from 1970 to the current time, and the second constructor, which defines a millisecond-level data as a date in date format.

3. Common methods

Many date manipulation methods are defined in date, but most are deprecated, with only a few remaining methods:

  /*** Returns The number of milliseconds since January 1, 1970, 00:00:00 GMT * represented by this <tt>date     </tt> object. *     * @returnThe number of milliseconds since January 1, 1970, 00:00:00 GMT * represented by this date. */     Public LongGetTime () {returnGettimeimpl (); }    /*** Sets this <code>Date</code> object to represent a, point in time, is * <code>time</     Code> milliseconds after January 1, 1970 00:00:00 GMT. *     * @paramThe number of milliseconds. */         Public voidSetTime (LongTime ) {Fasttime=Time ; CDate=NULL; }    /*** Tests If this date is before the specified date. *     * @paramWhen a date. * @return<code>true</code> if and only if the instant of time * represented by this <tt>date     </tt> object is strictly * earlier than the instant represented by <tt>when</tt>;     * <code>false</code> otherwise. * @exceptionNullPointerException if <code>when</code> is null. */     Public Booleanbefore (Date when) {returnGetmillisof ( This) <getmillisof (when); }    /*** Tests If this date was after the specified date. *     * @paramWhen a date. * @return<code>true</code> if and only if the instant represented * is <tt>date</tt> ;     object is strictly later than, the * instant represented by <tt>when</tt>;     * <code>false</code> otherwise. * @exceptionNullPointerException if <code>when</code> is null. */     Public BooleanAfter (Date) {returnGetmillisof ( This) >getmillisof (when); }

The four methods shown above are several common methods that are now in use in the date class:

Long GetTime () method: Returns the millisecond-level data from 1970 00:00:00 to the time represented by the Date object

void SetTime (Long time) method: Sets the point at which a Date object represents a millisecond level of data starting from 1970 00:00:00

Boolean before (date when) method: Determines whether a Date object represents a point in time before the point at which it represents

Boolean after (date) method: Determines whether the point in time represented by the date object is after the time point represented by when

4. Other

The date class implements the Java.io.Serializable interface, which can perform serialization and deserialization operations, defines the writeobject (ObjectOutputStream s) method and ReadObject in the date class ( ObjectInputStream s) method, respectively, to save and retrieve the timestamp (Long data) represented by the object when the Date object is serialized and deserialized, because the fasttime field is transient decorated Its contents are filtered out by the serialization mechanism, and this field holds the timestamp of the time represented by the Date object (long type)

For details, see theJava API Parsing-serialization API (i)

1     Private transient LongFasttime;2 3     /**4 * Save The state of this object to a stream (i.e., serialize it).5      *6      * @serialDataThe value returned by <code>gettime () </code>7 * is emitted (long). This represents the offset from8 * January 1, 1970, 00:00:00 GMT in milliseconds.9      */Ten     Private voidWriteObject (ObjectOutputStream s) One          throwsIOException A     { - S.writelong (Gettimeimpl ()); -     } the  -     /** - * Reconstitute This object from a stream (i.e., deserialize it). -      */ +     Private voidReadObject (ObjectInputStream s) -          throwsIOException, ClassNotFoundException +     { AFasttime =S.readlong (); at}

5. Example Analysis:

1      Public Static voidMain (string[] args) {2Date now =NewDate ();//Get current Time3Date when =NewDate (10201020097865L);//Specify point -in-time based on timestamp definition4         BooleanB1 =Now.after (when);5         BooleanB2 =Now.before (when);6Long D1 =now.gettime ();7Long D2 =when.gettime ();8         9System.out.println ("Now value is:" +Now );TenSystem.out.println ("When value is:" +When ); OneSystem.out.println ("B1 value is:" +B1); ASystem.out.println ("B2 value is:" +B2); -System.out.println ("D1 value is:" +D1); -System.out.println ("D2 value is:" +D2); the          -}

The result is:

The now value is: Thu Jul 13:39:12CST 16:41:37 CST 2293B1 value is:falseb2 value:true the D1 value is:1499319552116D2 value is:10201020097865

6. Summary

The date class is not recommended now, Java recommends calendar and DateFormat, and even simpledateformat to replace it, only a few of the remaining methods in date are still useful, especially before and after methods, can be very convenient to judge the order of two points of time, of course, the condition is to convert your time into the date format, using the date remaining two constructors to implement, Of course, you can also use the recommended SimpleDateFormat method for simple formatting of date format strings to get the time in date format, which will be learned later!

    

Java Basic Series--date class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.