Java date and time

Source: Internet
Author: User

Java date and time
Java date and time
Date class
The Date class and the Calendar class provided by Java are used to process the Date and time class, including the creation Date and time object, obtaining the current Date and time of the system.
. However, the Date class cannot be internationalized. Note that both the month and the hour start with 0, while the number of days in the month starts with 1, and the year is 1990
Year.
The main constructor provided by the Date class:
Date () generates a Date object that represents the current Date and time. The constructor calls System. currentTimeMillis () at the underlying layer to obtain a long integer as the Date parameter.
Date (long date): generates a Date object based on the specified long integer. The parameters of this constructor indicate the time difference between the created Date object and the GMT 00:00:00 object.
The unit of time is millisecond.
Method:
Long getTime (): return the long integer corresponding to the time, that is, the time difference between the object from GMT 00:00:00, in milliseconds as the unit of time
Void setTime (long time): set the time of the date object
Instance:
Public class DateTest {
Public static void main (String [] args ){
Date d1 = new Date ();
// Obtain the time of 100ms after the current time
Date d2 = new Date (System. currentTimeMillis () + 100 );
System. out. println (d2 );
System. out. println (d1.compareTo (d2 ));
}
}
Calendar class
Java provides better classes for processing Date and Time. Calendar is an abstract class that represents Calendar
Calendar is an abstract class. It is a template of the Calendar class and provides some common methods for all calendars.
You cannot use the constructor to create the Calendar class, but it provides several static getInstance () methods to obtain the Calendar Object.
These methods obtain specific Calendar based on the TimeZone and Locale classes.
The Calendar and Date classes can be freely converted.
// Create a default Calendar Object
Calendar calendar = Calendar. getInstance ();
// Retrieve the Date object from the Calendar
Date date = calendar. getTime ();
// Obtain the corresponding Calendar Object through the Date object
// Because the Calendar and GregorianCalendar do not have constructors to receive Date objects
// Therefore, you must first obtain a Calendar instance and then call its setTime () method.
Calendar calendar2 = Calendar. getInstance ();
Calendar2.setTime (date );
Calendar provides a large number of access methods to modify the date and time:
Void add (int field, int amount): Based on calendar rules, add or subtract the specified time for the specified field
Void roll (int field, int amount): similar to the add method, the difference is that the value of amount exceeds the maximum value that can be expressed by this field.
The range is not automatically carried up.
Void set (int field, int value): set the given calendar field to the given value
Void set (int year, int month, int date): sets the given calendar field to the given value.
Void get (int field): returns the values of the year, month, and date fields of the specified calendar field.
Public class CalendarTest {
Public static void main (String [] args ){
Calenda calendar = Calendar. getInstance ();
System. out. println (calendar. get (YEAR ));
System. out. println (calendar. get (MONTH ));
// 2004-10-23 -\
System. out. println (calendar. set ));
System. out. println (calendar. get (Date ));
// Push the Calendar back for one year
System. out. println (calendar. add (YEAR, 1 ));
// Push the Calendar forward for 8 months
System. out. println (calendar. roll (MONTH, 8 ));
}
}
// YEAR, MONTH, and DATE are static constants in 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.