Date, calendar, dateformat, and simpledateformat in Java

Source: Internet
Author: User

Java. util. Date: indicates a specific moment, accurate to milliseconds. Before jdk1.1, the date is interpreted as the year, month, day, hour, minute, and second values, but its functions are not easy to internationalize. Therefore, from jdk1.1, use the calendar class to convert the date and time fields, and use the dateformat class to format and parse the date string. Void settime (Long Time): sets this date object.

Java. util. calendar: an abstract class that provides some methods to convert a specified moment to a group of calendar fields such as year, month, day_of_month, and hour, and operate calendar fields (for example: obtain the date of the next week. Use: Calendar rightnow = calendar. getinstance (); to instantiate the calendar ();. The common subclass is gregoriancalendar.

Java. Text. dateformat: is an abstract class of the date/time format subclass (for example, simpledateformat). It formats and parses the date or time in a language-independent manner.

Java. text. simpledateformat: it is a language environment-related method to format and parse a specific class of date, date/time format subclass, run to format (I .e.: Date-> text), parsing (text-> date), and standardization. Main method: Date parse (string text, parseposition POS): parses the string text to generate date.

Calendar calendar = calendar. getinstance (); // static method Initialization

Calendar. settime (date); // This method is used to pass the date object in

The following method is used to obtain various attributes of Date:

Int year = calendar. Get (calendar. year );

Int month = calendar. Get (calendar. month) + 1; // do not forget to add 1

Int day = calendar. Get (calendar. Day); // There is also day_of_month, which must be distinguished.

Another usage is to change the date:

Calendar. Set (field, value); // sets a field to a specified value. Note that the value does not take effect before calling get.

Calendar. Add (field, Delta); // used to add a day or reduce it by one year, which takes effect immediately

Calendar. Roll (field, Delta); // the difference between it and add is that larger field will not change. Generally, add

Date = calendar. gettime (); // after successful completion, the changed date is obtained.

Simpledateformat exists for conversion between string and date.

Simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");

From string to date:

Date = DF. parse (string datestring); // throws parseexception

Generally, the value of date is represented by the number of milliseconds from 00:00:00 (GMT 0) to the present.

Date = new date ();

Int time = date. gettime (); // used for subtraction to obtain the Time Difference Accurate to Ms.

Boolean isequal = date. Equals (anotherdate); // checks whether the two times are equal.

Date. compareto (anotherdate );


Related Article

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.