Java Learning--using the date and SimpleDateFormat classes to represent time

Source: Internet
Author: User
Tags date now

Using the date and SimpleDateFormat classes to represent time

In program development, it is often necessary to deal with date and time related data, when we can use the date class in the Java.util package. The main function of this class is to get the current time, let's look at the use of the Date class:

The object created by using the default parameterless construction method of the date class represents the current time , and we can directly output a Date object that displays the current time, showing the following results:

Among them, Wed represents Wednesday (Wednesday), June stands for June (June), 11 for 11th, CST for China Standard Time (Beijing time, the East eight district).

From the above output, we found that the default time format is not very friendly, and we see the date format is not the same, if you want to display in the specified format, such as 2014-06-11 09:22:30, then how to do?

At this time to the java.text package of SimpleDateFormat class to do their time!! You can use SimpleDateFormat to format datetime, such as to convert a date to text in a specified format, or to convert text to a date.

1. Use the format () method to convert a date to a text in the specified format

The code "YYYY-MM-DD HH:mm:ss" is a predefined string, YYYY represents four-bit years, MM for two-bit months, DD for two-bit dates, HH for hours (using 24-hour system), MM for minutes, SS for seconds, so that the target format of the transformation is specified, and the last Call The format () method converts the time to a string of the specified format.

Running result: 2014-06-11 09:55:48

2. Use the parse () method to convert text to a date

The "yyyy mm DD day HH:mm:ss" in the code specifies the date format of the string, and the parse () method is called to convert the text to a date.

Operation Result:

Must pay attention to OH:

1. A conversion exception may occur when invoking the parse () method of the SimpleDateFormat object, or ParseException, so exception handling is required

2. You need to import the Java.util package when using the Date class, you need to import the Java.text package when using SimpleDateFormat

Task

Are you familiar with the use of the Date class and the SimpleDateFormat class? Let's check it out.

Function: Implements text that converts a date to a specified format, converts the text to a date

Demo:(note To add a header file first OH)

Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

         Public Static voidMain (string[] args) throws ParseException {//use the Format () method to convert a date to text in the specified formatSimpleDateFormat SDF1 =NewSimpleDateFormat ("yyyy mm month dd Day hh mm min ss sec"); SimpleDateFormat SDF2=NewSimpleDateFormat ("YYYY/MM/DD hh:mm"); SimpleDateFormat sdf3=NewSimpleDateFormat ("YYYY-MM-DD HH:mm:ss"); //creates a Date object that represents the current timeDate now =NewDate (); //call the Format () method to convert the date to a string and outputSystem. out. println (Sdf1.format (now)); System. out. println (Sdf2.format (now)); System. out. println (Sdf3.format (now)); //convert text to a date using the parse () methodString d ="2014-6-1 21:05:36"; SimpleDateFormat SDF=NewSimpleDateFormat ("YYYY-MM-DD HH:mm:ss"); //Call the Parse () method to convert the string to a dateDate Date =Sdf.parse (d); System. out. println (date); }

Operation Result:

Java Learning--using the date and SimpleDateFormat classes to represent time

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: 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.