Using the date and SimpleDateFormat classes to represent time

Source: Internet
Author: User
Tags date now

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, and let's look at the use of the date class:

Date D=new date ();//Use the default constructor method to create a Date object System.out.println (d);//Output Date Object

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

Fri APR 13:57:05 CST 2015

Among them, Fri stands for Friday, Apr for April, 24 for 24th CST to represent China standard Time is Beijing time, East eight district.

From the above output, we found that the default time format is not very friendly (nor we want to display), at this time we use the Java.text package Simpleddateformat class, you can use the SimpleDateFormat To format a datetime, such as a date that can be swapped for text in the specified format, or to convert the text to a date.

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

// Create a Date object to represent the current time Date d=new  date (); // Create a SimpleDateFormat object to specify the destination format SimpleDateFormat sdf=New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); // call the Format () method, format the time, and convert the string to the specified format String today=Sdf.format (d); // output the converted string System.out.println (today);

Output: (now to can guess Hey): 2015-04-24 14:07:03

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

// Create a string of date format String day= "April 24, 2015 14:13:50"; // creates a Simpledatefomat object that specifies the date format of the string SimpleDateFormat df=New SimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); // Call the Parse () method to convert the string to a date Date d=df.parse (day); // Output System.out.println ("Current time:" +d);

Output: Current time: Fri APR 14:13:50 CST 2015

Finally: Here's a demo of both:

Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classHelloWorld { Public Static voidMain (string[] args)throwsParseException {//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); }}

Using the date and SimpleDateFormat classes to represent time

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.