[Java] Common date processing

Source: Internet
Author: User
Tags add time dateformat set time time and seconds time in milliseconds

Recent review of the Java Foundation, found in Java about date processing classes are messy, novice often can't find the right method. So decided to write a blog about the common Java date processing.

The date-related classes commonly used in Java, and the relationship between them as shown, in which the middle of date refers to Java.util.Date, and the lower-left date refers to Java.sql.Date. These classes contain almost all of the common date-processing methods, which are described in detail below.


1.Date (Java.util.Date)

The date class should be the first class in Java to be used for date processing, and its parameterless constructor can create an object representing the current time, and other constructors also support the construction of a Date object by the date of year or string of type int, as shown in the following code:

Create the current time object by default constructor Date D = new Date ();//Create object by date of type int Date d = new Date (2014, 7, 30);//Create object by date of string ("Sat Oct 2014 14:59:00");
However, it is a pity that The following two methods are replaced in the Java1.1 version by the calendar's set method and the parse method of DateFormat, and all date modification methods in the date class are replaced by the corresponding methods in the calendar, which means that the date class will no longer be used for the modification of dates, only as a package date Provided by the class of the data.


2.Date (Java.sql.Date), time, and timestamp

The subclasses of these three date classes are often encountered in database operations and are used to represent the date (month and day), the time (time and seconds), and the DateTime (month, day, and minute). They are generally used to hold date data that is taken out of the database, and not much is introduced here.


3.Calendar

Because the date class is not good for internationalization support, the Calendar class is introduced in the Java1.1 version, and the Chinese translation is the meaning of calendars. The calendar is an abstract class, and we need to use its static method getinstance to get an instance of its implementation class representing the current time, as shown in the following code:

Get the calendar instance through getinstance calendar RightNow = Calendar.getinstance ();
The Calendar class provides a considerable number of methods and static variables for date processing, and its get and set methods require fields to set and remove corresponding values, such as:

Set the required time by setting method Rightnow.set (Calendar.hour_of_day, 15);//Get set time by the Get method int nowhour = Rightnow.get (calendar.hour_of _day);
In addition, the calendar also provides a way to add time to add, such as:

Add 15 minutes to Rightnow.add (Calendar.minute, 15) on the basis of the current time;
Calendar There are many other functions that can modify the date, you can query the API documentation, this article does not detail.


4.DateFormat

The DateFormat class is an abstract class of date/time formatting subclasses that formats and resolves dates or times in a language-independent manner. In practical use we usually use its subclass SimpleDateFormat.

The SimpleDateFormat constructor can specify a date format and then parse or output the Date object in this date format, such as:

Creates a SimpleDateFormat object that specifies a date format of year-month-day. SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");//Resolution Date D = Sdf.parse ("2014-8-30");// Create another SimpleDateFormat, specifying the format for XXXX year XX month XX day. SDF = new SimpleDateFormat ("yyyy year mm DD Day");//conversion date string s = Sdf.format (d);

4. Other

The TimeZone class represents the time zone offset, which is not explained in detail by using less, and is interested in querying the API documentation.

The system class has a Currenttimemillis method that can return the current time in milliseconds, which is typically used to calculate the program difference.


5. Summary

You can use the Java.util.Date class and its subclasses Java.sql.Date class, Time class, and timestamp class when you need to save the date.

The Calendar class is recommended when you need to change the date.

It is recommended to use the SimpleDateFormat class when formatting parsing or outputting dates.



[Java] Common date processing

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.