Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - The use of date classes and methods in java language
The use of date classes and methods in java language
Date and Calendar are classes that provide time processing in the Java class library. Because date plays an important role in the application of business logic, I want to make a basic explanation of these two classes here. Please correct me because of limited technology.

As the name implies, the Date class is a date-related class. The main function of this class is to get the current time. However, this class also has the function of setting time and other functions. However, due to their own design problems, these methods have been criticized by many people, and these criticized functions have been transplanted to another class, that is, the second class calendar to be talked about today.

Before we talk about the two classes, we have to mention another class here, that is, the DateFormat class, which is used to format dates, which will be mentioned later.

First, let's look at an example of getting the current time:

Date Date = new Date();

system . out . println(date . gettime()); The above statement first creates a Date object, and then uses the getTime method to get the current time, but notice that the output result is indeed a long integer. Why? In fact, this is a long number calculated by the system according to the current time. As for how to calculate, this article will not say. So, how to display the correct time? This will take advantage of the DateFormat class above, which is a base class and has a subclass of SimpleDateFormat. Please see the following code for specific usage:

Date Date = new Date();

SimpleDateFormat dateFm = new SimpleDateFormat(" EEEE-MMMM-DD-yyyy ");

system . out . println(datefm . format(date)); This code begins by creating a Date object to get the current time, focusing on the SimpleDateFormat object. This pair inherits DateFormat, Formats the Date object using the format method, and then outputs it. The format is customized by users, EEEE stands for week, MMMM stands for month, dd stands for day, and yyyy stands for year. This method can be used to output time according to a user-defined format.

The output time of custom format is introduced above, and the standard format output time provided by JAVA class library will be introduced in the future, and DateFormat class will be used. Please look at the following code:

Date Date = new Date();

date format dateFm = date format . getdatetime instance(date format。 Short,

Date format. Short);

system . out . println(datefm . format(date)); The method used here is similar to the user-defined method, except that an abstract class is used here. Because DateFormat is an abstract class, it cannot construct an object through a constructor. Here, the object is obtained by the getDateTimeInstance () method, and the passed parameters are some constants defined in DateFormat. The system outputs the current time according to these constants. Because the getDateTimeInstance method is used here, two constant parameters will be passed to format the date and the current time respectively.

The above describes how to get the system time and how to print the format. What if you want to get or set a certain part of time? Such as year, month and day. It depends on the Calendar class, which is also an abstract class and has a subclass GregorianCalendar. Next, I will use this subclass to demonstrate this process. Please look at the following code:

date format date format = date format . getdate instance(date format。 Full);

Gregorian calendar cal = new Gregorian calendar();

Cal.setTime (new date ());

System.out.println ("system date:"+dateformat.format (cal.gettime ()));

cal.set(GregorianCalendar。 What day is the Gregorian calendar? Friday);

System.out.println ("Set the day of the week after Friday:"+

date format . format(cal . gettime());

In this code, first create a DateFormat object for Formatting, then create a GregorianCalendar object cal, then use the cal.setTime () method to set the time in the cal object as the current time, and then print format the time returned by cal.getTime () according to the format. Later, the date of cal is set to Friday of the current week by using the set method, and the time stored in cal is this time of Friday. Later, the print format of Format was used. If the current time is 1 1: 30 on Thursday, October 27th, 2005, then the last sentence will be output as 65438+65438+ on Friday, October 28th, 2005.