The methods of date classes in Java, Java date class methods

Source: Internet
Author: User

The methods of date classes in Java, Java date class methods

For more information, see,thank you.

This article mainly introduces four types: Date, DateFormat, SimpleDateFormat, and Calendar.

Date class:

Under the java. util package

The Date type indicates a specific moment, accurate to milliseconds.
From JDK 1.1, use the Calendar class to convert the date and time fields, and use the DateFormat class to format and analyze the date string. The corresponding method in Date has been discarded.

So Date is mainly used to generate the time,

Date has two constructor Methods

Date ()
AllocateDateObject and initialize the object to indicate the time (precise to milliseconds) it is allocated ).

Date (long date)

AllocateDateObject and initialize this object to indicate the specified number of milliseconds since the standard reference time (called epoch), that is, January 1, 1970 00:00:00 GMT.

Date date = new Date();Date date2=new Date(0);System.out.println(date);System.out.println(date2);

The output result is:

Tue Dec 16 19:40:32 CST 2014Thu Jan 01 08:00:00 CST 1970

We can see the difference between the two constructors. Here I personally think that the second constructor is of little use, because it is unrealistic to know the number of milliseconds for a time relative to "calendar.

Common Date methods:

Boolean after (Date when) test whether the Date is after the specified Date
Boolean before (Date when) test whether the Date is earlier than the specified Date
Int compareTo (Date anotherDate) compares the order of two dates
Long getTime () returns the number of milliseconds represented by the Date object since 00:00:00 GMT, January 1, January 1, 1970.
Void setTime (long time) sets this Date object to indicate the time point in milliseconds after January 1, January 1, 1970 00:00:00 GMT

I feel that these methods are meaningless, so I will not introduce them.

DateFormat class:

Under the java. text package

DateFormat is an abstract class of the date/time formatting subclass. It formats and parses the date or time in a language-independent manner.

It is an abstract class, so it cannot be instantiated by the constructor. You can use the static functions getDateInstance () and getDateTimeInstance () to instantiate it. The difference between the two is that one returns a date and the other returns a date + time.

Meanwhile, getDateInstance (int style, Locale aLocale)

About the style value:

FULL: the maximum length, for example, Wednesday, January 1, January 9, 2013.
LONG: The length is longer, for example, January 9, 2013.
MEDIUM: The length is longer than SHORT, for example, Jan 9,2013.
SHORT: it is a number, for example, 13/1/9.

Let's take a look at the example:

DateFormat df = DateFormat. getDateInstance (); // use getDateTimeInstance to generate a format instance, so that both date and time can be displayed as DateFormat df2 = DateFormat. getDateTimeInstance (); String s = df. format (date); String s2 = df2.format (date); System. out. println (s); System. out. println (s2 );
The output result is

2014-12-162014-12-16 20:01:05

We can see that there is a date, a time, and only a date.

DateFormat df3 = DateFormat. getDateInstance (DateFormat. FULL, Locale. CHINA); String china_time = df3.format (new Date (); DateFormat df4 = DateFormat. getDateInstance (DateFormat. LONG, Locale. CHINA); String g_time = df4.format (new Date (); System. out. println ("full value:" + china_time); System. out. println ("long value:" + g_time );

The output value is:

Full value: Tuesday, January 1, December 16, 2014 long value: January 1, December 16, 2014

I will not explain it here.

SimpleDateFormat class:

Under the java. text package

It is a direct subclass of the DateFormat class and inherits the DateFormat class. I understand the SimpleDateFormat class in this way. It is more grounded than the Datef class. You can assign it a date of the form and change it at will.

The SimpleDateFormat class is mainly used to convert the formats between dates, and the following steps are required during the conversion process:
1. Specify a template and retrieve the first All time numbers based on the template.

2. All time numbers are saved in the Date class.
3. convert all the time numbers in the format again.
The following table lists the templates, which are case sensitive.

Date Template Description
Year Y Year: yyyy
Month M Month: MM
Day D Indicates day: dd
Hour HH Time: HH
Minute Mm Mm
Seconds Ss Second: ss
Millisecond S Millisecond: SSS

Date date2 = new Date (); SimpleDateFormat spf = new SimpleDateFormat ("yyyy-MM-dd: HH-mm-ss seconds"); System. out. println (spf. format (date2 ));

October 16-October 16:--49 seconds

There are two methods here. Make sure you understand them. One is spf. format (Date), one is spf. parse (String) represents the Date type, which is extracted from the specified String and is converted into a new format.

At the same time, exceptions may occur when calling the parse method,

Try {

} Catch (ParseException e ){
E. printStackTrace ();

This should not be difficult to understand. It may capture exceptions if the string cannot be extracted from the date.

Calendar class:

Under the java. util package

The Calendar class is an abstract class,

It provides some methods to convert "specific moments" to a group of calendar fields such as "YEAR", "MONTH", "DAY_OF_MONTH", and "HOUR, it also provides some methods to operate calendar fields (such as obtaining the date of the next week.

There are two ways to instantiate Calendar: the first is Calendar nowTime = new GregorianCalendar (); the second is Calendar calendar = Calendar. getInstance ();


Calendar nowTime = new GregorianCalendar (); // gets the default date, time, and time zone of the current system platform. Int todayYear = nowTime. get (Calendar. YEAR); // obtain the "YEAR" of the default date on the current system platform ". Int todayMonth = nowTime. get (Calendar. MONTH) + 1; // obtain the "MONTH" of the default date on the current system platform ". Int todayDay = nowTime. get (Calendar. DAY_OF_MONTH); // obtain the "day" of the default date on the current system platform ". // Output the time of the current system platform. System. out. println ("the current date is:" + todayYear + "year" + todayMonth + "month" + todayDay + "day"); nowTime. set (2013, 2-1, 9); System. out. println (nowTime. getTime (); Calendar calendar3 = Calendar. getInstance (); calendar3.add (Calendar. DATE, 10); int year1 = calendar3.get (Calendar. YEAR); // month int month1 = calendar3.get (Calendar. MONTH) + 1; // Date int date1 = calendar3.get (Calendar. DATE); System. out. println (year1 + "year" + month1 + "month" + date1 + "day ");


The current date is: December 16, 2014 Sat Feb 09 20:29:30 CST 20132014-December 26

Call... Finally, it's finished ....

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: 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.