SimpleDateFormat format date usage in Java

Source: Internet
Author: User

Import java. text. SimpleDateFormat;
Import java. util .*;
 
Public class SimpleDateFormatDemo {
 
Public static void main (String [] args ){
Time (); // call the time () method
Time2 (); // call the time2 () method
Time3 (); // call the time3 () method
}
 
// Obtain the current date (in the 24-hour format)
Public static void time (){
 
SimpleDateFormat sdf = new SimpleDateFormat (); // format the time
Sdf. applyPattern ("yyyy-MM-dd HH: mm: ss a"); // a indicates the am/pm mark
 
Date date = new Date (); // obtain the current time
System. out. println ("current time:" + sdf. format (date); // output the formatted current time (in 24-hour format)
}
 
// Obtain the current time (in 12-hour format)
Public static void time2 (){
 
SimpleDateFormat sdf = new SimpleDateFormat (); // format the time
Sdf. applyPattern ("yyyy-MM-dd hh: mm: ss ");
 
Date date = new Date ();
System. out. println ("current time:" + sdf. format (date); // output the current time of formatting (in 12-hour format)
}
 
// Obtain the date after 5 days
Public static void time3 (){
 
SimpleDateFormat sdf = new SimpleDateFormat (); // format the time
Sdf. applyPattern ("yyyy-MM-dd HH: mm: ss ");
/*
* The Calender class is an abstract class. Therefore, it cannot be instantiated by using the new method. You need to instantiate it by using subclasses that have already been implemented by the abstract method.
* A class method getInstance is provided to obtain a common object of this type.
*/
Calendar calendar = Calendar. getInstance ();
Calendar. add (Calendar. DATE, 5); // add 5 days to the current DATE
 
Date date = calendar. getTime ();
 
System. out. println ("five days later:" + sdf. format (date); // output the time of five days later.
}
}

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.