Date and calendar are classes in the Java class Library that provide time processing, and because dates occupy a very important place in the application of business logic, here you want to give a basic explanation of these two classes, because the technology is limited, please correct me.
The date class, as its name suggests, knows the class that is related to the date, and the main function of this class is to get the current time, but the class also has set time and some other functions, but because of its own design problems, these methods have been criticized, And these critical functions have been transplanted into another class, which is the second class calendar to be mentioned today.
Before you talk about two classes, there's no way to mention one more class, which is the DateFormat class, which is used to format dates, and later on.
First, let's look at an example of getting the current time:
Date date = new Date ();
System.out.println (Date.gettime ());
Because I am lazy, write some of the main statements on the line, the above statement first created a Date object, and then use the GetTime method to get the current time, but note that the output after the result is a string of long integer numbers, this is why? In fact, this is the system based on the current time calculation of the number of a long, as to how the calculation is not in this article, so how to show the correct time? This is going to take advantage of the above DateFormat class, which is a base class that has a subclass that is SimpleDateFormat, and see the following code for specific use:
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, with the emphasis on the later SimpleDateFormat object, which inherits the DateFormat, formats the date object with the Format method, and then outputs the The format customization is customized by the user, eeee represents the week, mmmm represents the month, and DD represents the day, yyyy represents the year. Using this method, you can output time according to the user-defined format.