The date class of Java and the calendar

Source: Internet
Author: User
Tags date1 time and date

First, Date class

In JDK1.0, the date class is the only class that represents time, but since the date class is not easy to internationalize , starting with the JDK1.1 version, it is recommended to use the Calendar class for time and date processing. Here is a brief introduction to the use of the date class.

  1. Use the date class to represent the current system time

New Date ();

Objects created using the default constructor method of the date class represent the current time, because the date class overrides the ToString () method. So you can output the object of date type directly, the result is as follows:

Thu Jan 18:32:40 CST 2018

In this format, Thu represents Thursday (Thursday), and Jan represents Janauary (January), 11 for 11th, and CTS on behalf of China Standard Time, which is the Beijing Times (East eight zone).

  2. Use the date class to represent the specified time

Date Date=new date (2017-1900,1-1,11);
SYSTEM.OUT.PRINTLN (date);

Using a constructed method with parameters, you can construct a date object of the specified day, andthe parameters of the year in the date class should be the year minus 1990 that actually needs to be represented, minus the value after 1 for the month actually represented . The results appear as follows:

Wed Jan 00:00:00 CST 2017

The actual representation of the date of the month and minute of the day object and this similar

  3. Get information from a Date object

 PackageCom.sc.util;Importjava.util.Date; Public classDatetest { Public Static voidMain (string[] args) {Date d=NewDate (); intYear = D.getyear () +1990; intMonth=d.getmonth (+1); intDate=d.getdate (); intHour=d.gethours (); intMinute=d.getminutes (); intSecond=d.getseconds (); intday=D.getday (); System.out.println ("Year:" +Year ); System.out.println ("Month:" +month); System.out.println ("Date:" +date); System.out.println ("Hours:" +hour); System.out.println ("Minutes:" +minute); System.out.println ("Seconds:" +second); System.out.println ("Week:" +Day ); }}
View Code

Using the Get method in the date class, you can obtain information about the Date class object (but you can see that the methods in the date class are obsolete).

  It is important to note that using getyear to obtain a value after the year minus 1900 in the Date object, so it is necessary to display the corresponding year by adding 1900 on the basis of the return value, similar to the month. The Getday method is also provided in the date class to obtain a Date object representing the day of the week, and the date class specifies that Sunday is 0, Monday is 1, Tuesday is 2, followed by and so on.

  4, the Date object and the relative time of the mutual transfer

  (1) Convert the object of the date class to relative time

D=new Date ();
Long t=d.gettime (); System.out.println (t);

The result is:

1515671897851

  (2) Convert relative time to object of date class

long time = 1290876532190Lnew  Date (time); System.out.println (DA);

The result is:

Sun Nov 00:48:52 CST 2010

  5. Comparison between date objects

New Date (2016-1900,8-1,28new  date (); System.out.println (Date.compareto (date1));

The result is:

-1

  Date.compareto (date1) returns the int type. If it is equal to 0, then date=date1, or date<date1; if less than 0

  Summary: Using the GetTime method in a Date object, you can convert an object of the date class to a relative time, using the construction method of the date class to convert the relative time to the object of the date class. After the conversion, it is convenient for the calculation of time, but also makes the time display more intuitive.

Second, Calendar class

Starting with the JDK1.1 version, it is recommended that the Calendar class be implemented when the date and time is processed (some methods of date are obsolete). In design, the function of the calendar class is much more powerful than the date class, and in the implementation of the method is more complex than the date class, the following describes the use of the Calendar class.

The Calender class is an abstract class that implements the object of a particular subclass when it is actually used, and the process of creating the object is transparent to the programmer and only needs to be created using the getinstance () method .

  1. Use the Calendar class to represent the current time

Calendar C = calendar.getinstance (); System.out.println (c);   // The return is a calendar object

The result is:

Java.util.gregoriancalendar[time=1515673764154,arefieldsset=true, areallfieldsset=true, Lenient=true, zone=sun.util.calendar.zoneinfo[id= "Asia/shanghai", Offset=28800000,dstsavings=0, Usedaylight=false, transitions=19,lastrule=null],firstdayofweek=1,minimaldaysinfirstweek=1, Era=1,year=2018,month=0,week_of_year=2,week_of_month=2,day_of_month=11,day_of_year=11,day_of_week=5,day_of_ Week_in_month=2,am_pm=1,hour=8,hour_of_day=20,minute=29,second=24,millisecond=154,zone_offset=28800000,dst_ Offset=0]

  Note: because the Calendar class is an abstract class, and the Calendar class is constructed protected, you cannot create objects using the Calendar class's constructor, which provides the GetInstance method for creating objects. using this method to obtain the Calendar object represents the current system time, because the Calendar class ToString implementation of the No date class is so intuitive, so the direct output of the Calendar class object is not very meaningful.

 2. Use the Calendar class to represent the specified time

Calendar C1 = calendar.getinstance (); C1.set (2016,8-1,28);

 Using the Calendar class to represent a specific time, you need to first create a Calendar object, and then set the month-day parameter in the object to complete.

The declaration of the Set method is: Public final void set (int year,int month,int date)

The example code above is set to August 28, 2016 , and its parameters are not the same structure as the date class. The values of the year in the Calendar class are written directly, the value of the month is the actual month value minus 1, and the date value is the actual date value.

  If you only set a field for a field, such as a value for a date, you can use the following set method:

 Public void Set (int field,int value)

In this method, the parameter field represents the type of field to be set, and the common types are as follows:

(1)calendar.year--- year, (2)calendar.month--- month, (3)calendar.date--- date, (4)calendar.day_of_month---date, exactly the same as the Calendar.date field;
(5) The number of hourscalendar.hour---12-hour system;
(6) The number of hourscalendar.hour_of_day---24-hour system;
(7)Calendar.minute---minutes;
(8)Calendar.second---seconds;
(9)Calendar.day_of_week---Day of the week.

The subsequent parameter value represents the value set to.

For example: C1.set (calendar.date,10); The purpose of this code is to set the date represented by the C1 object to number 10th, and all other values will be recalculated, such as the day of the week and the corresponding relative time value.

  3. Get the information in the Calendar class

 PackageCom.sc.util;ImportJava.util.Calendar; Public classDatetest { Public Static voidMain (string[] args) {Calendar C=calendar.getinstance (); intYear=C.get (c.year); intMonth=c.get (C.month) +1; intDate=C.get (C.day_of_month); intHour=C.get (C.hour_of_day); intMinute=C.get (C.minute); intSecond=C.get (C.second); intday=C.get (Calendar.day_of_week); System.out.println ("Year" +Year ); System.out.println ("Month" +month); System.out.println ("Date" +date); System.out.println ("Hours" +hour); System.out.println ("Minutes" +minute); System.out.println ("Seconds" +second); System.out.println ("Week" +Day ); }}
View Code

In the Calendar class, Sunday is 1, Monday is 2, Tuesday is 3, and so on.

  4. Other method description

In fact, the Calendar class also provides a lot of other useful methods, the following simple introduction of a few common methods of use.

(1) Add Method

 Public Abstract void Add (int field,int amount)

  The function of this method is to increase or decrease a certain number of values on a field in the Calendar object, increase the value of amount to be positive, and decrease the value of amount to negative.

For example, calculate the date after 100 days of the current time with the following code:

Calendar C3 = calendar.getinstance (); C3.add (calendar.date,+); int year =int month = c3.get (calendar.month); int Date  = c3.get (calendar.date); System.out.println (year + "Years" +month+ "month" +date+ "Day");

The Add method here refers to the calendar.date of the C3 object, which is the addition of 100 on the Date field, and the value of the other fields in the Date object is recalculated within the class to obtain a date after 100 days, for example, the output of the program may be: March 21, 2018

  (2) After method

 Public boolean after (Object when)

The function of this method is to determine whether the current date object is behind the When object, or False if it returns true after the When object. For example:

Calendar C4 = calendar.getinstance (); C4.set (2016,8-1,28= Calendar.getinstance (); C5.set (2016,10-1,1); Boolean b = c5.after (c4); System.out.println (b);

In the sample code, the time represented by the object C4 is August 28, 2016, and the time the object C5 represents is October 1, 2016, then the date represented by the object C5 is after the date represented by the C4, so the return value of the After method is true.

  Another similar approach is before, which is to determine whether the current date object is in front of another date object

  5. Conversion between calendar object and relative time

(1) Convert Calendar object to calendar object

The date class of Java and the calendar

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.