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