Use the date class and the format class to format the current time of the system

Source: Internet
Author: User

One: Date------------String

Code 1: (Code two to optimize the displayed time format)

 PackageDatedemo;ImportJava.text.SimpleDateFormat;Importjava.util.Date;//Requirements: Print out the time of the current system//Required Classes: Date class: Generate Current system time//SimpleDateFormat class: Format the generated system time//Construction Method Summary SimpleDateFormat () Public classDatedemo { Public Static voidMain (string[] args) {//Create a dateDate d=NewDate (); //Create a subclassSimpleDateFormat str =NewSimpleDateFormat (); //using the method of the Str parent class, Format (date date) formats a date as a date/time string. //calling the parent class method formatString time=Str.format (d);    System.out.println (time); }}

Code two: An optimized output for the displayed time format

Package Datedemo;import java.text.simpledateformat;import java.util.date;//requirements: Print out the current system time//Required class: D Ate class: Generate current system time//SimpleDateFormat class: Format//construct method summary for generated system time SIMPLEDATEFO Rmat () public class Datedemo {public static void main (string[] args) {//Create a date d=new date ();//system.out.println (d);        /fri Sep 18:42:21 CST 2016//Create a subclass//simpledateformat str =new simpledateformat (); Using the method of the Str parent class, Format (date date) formats a date as a date/time string.              Call the parent class method format//string Time=str.format (d); System.out.println (time);//16-9-9 6:42//optimize: The format of the display is: September 9, 2016 18:42:21//yyyy MM month DD Day KK:MM:SS//custom method for parsing//method of String: SimpleDateFormat (string pattern) constructs a date format symbol for a given pattern and default locale Simpledateform        At        SimpleDateFormat str =new simpledateformat ("yyyy mm month DD Day KK:mm:ss");        String Time=str.format (d);        System.out.println (time); }}


Two  : String------------Date


 PackageDatedemo;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;//Requirements: Print out the time of the current system//Required Classes: Date class: Generate Current system time//SimpleDateFormat class: Format the generated system time//Construction Method Summary SimpleDateFormat ()//Date------String//////String----Date Public classDatedemo { Public Static voidMain (string[] args)throwsParseException {//Create a date//Date D=new date (); //System.out.println (d);//Fri Sep 18:42:21 CST//Create a subclass//SimpleDateFormat str =new simpledateformat (); //using the method of the Str parent class, Format (date date) formats a date as a date/time string. //calling the parent class method format//String Time=str.format (d); //System.out.println (time);//16-9-9 pm 6:42//optimize: Make time display format: September 9, 2016 18:42:21//yyyy mm month DD Day KK:MM:SS//custom methods for parsing strings//method: SimpleDateFormat (String pattern) constructs a simpledateformat with a given pattern and a date format symbol for the default locale. //SimpleDateFormat str =new simpledateformat ("yyyy mm month DD Day KK:mm:ss");//String Time=str.format (d);//System.out.println (time);//System.out.println ("------------------"); //String-------Date//format a string as a date format//Call Method: Date Parse (string source) parses the text from the beginning of the given string to generate a date. String str= "September 9, 2016 18:42:21"; Date DD=NewDate (); SimpleDateFormat d=NewSimpleDateFormat ("yyyy mm month DD Day KK:MM:SS"); DD=d.parse ("September 9, 2016 18:42:21");                    SYSTEM.OUT.PRINTLN (DD); }}
















Use the date class and the format class to format the current time of the system

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.