Basic usage of Java date and calendar classes

Source: Internet
Author: User
Tags current time dateformat integer numbers set time

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.

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.