Java SE basics-SimpleDateFormat (Date Format) and sesimpledateformat for common class libraries

Source: Internet
Author: User

Java SE basics-SimpleDateFormat (Date Format) and sesimpledateformat for common class libraries

Get the current date and format the input according to different dates. The Code is as follows:

1 // 20160618 SimpleDateFomat class use date formatting Exercise 2 package MyPackage; // custom package 3 4 import java. text. simpleDateFormat; // The SimpleDateFormat package required for import 5 import java. util. date; // import the required Date Package 6 7 class MyDateDemo {// defines the MyDateDemo class 8 private SimpleDateFormat sd = null; // declares the SimpleDateFormat object sd 9 10 public String getDate01 () {// define the getDate01 method 11 this. sd = new SimpleDateFormat ("yyyy-MM-dd HH: mm; ss. sss "); // get a" yyyy-MM-dd12 // HH: mm; ss. sss "format date 13 return this. sd. format (new Date (); // format the current Date 14} 15 16 public String getDate02 () {// define the getDate02 Method 17 this. sd = new SimpleDateFormat ("MM mm dd, yyyy hh mm mm ss s sss ms "); // get a format of "yyyy MM dd, yyyy, 18 // HH mm min ss sec sss millisecond" 19 return this. sd. format (new Date (); // format the current Date 20} 21 22 public String getDate03 () {// define the getDate03 Method 23 this. sd = new SimpleDateFormat ("yyyyMMddHHmmsssss"); // get a "yyyyMMddHHmmsssss" format date (that is, the timestamp) 24 return this. sd. format (new Date ()); // format the current date 25} 26} 27 28 public class SimpleDateFormatDemo {// main class 29 30 public static void main (String [] args) {// main method 31 MyDateDemo dd = new MyDateDemo (); // declare the dd object and instantiate the 32 System. out. println ("default Date format:" + new Date (); // call the method to enter a Date of different formats 33 System. out. println ("English Date Format:" + dd. getDate01 (); 34 System. out. println ("Chinese Date Format:" + dd. getDate02 (); 35 System. out. println ("timestamp:" + dd. getDate03 (); 36 37} 38 39}

 

There is another date formatting method, which is preferred. The code for another method is as follows:

 

1 import java. util. *; // import the required toolkit 2 class DateTime {// you can directly use this class to obtain the date and time 3 private Calendar calendar = null; // declare a Calendar Object, get time 4 public DateTime () {// The object is instantiated in the constructor 5 this. calendar = new GregorianCalendar (); 6} 7 public String getDate () {// you can get a date in the format of yyyy-MM-dd HH: mm: ss. SSS 8 // considering that the program needs to modify strings frequently, use StringBuffer to improve performance 9 StringBuffer buf = new StringBuffer (); 10 buf. append (calendar. get (Calendar. YEAR )). append ("-"); // increase the number of buckets by 11 years. append (this. addZero (calendar. get (Calendar. MONTH) + 1, 2 )). append ("-"); // increase by 12 buf months. append (this. addZero (calendar. get (Calendar. DAY_OF_MONTH), 2 )). append (""); // get the 13 buf. append (this. addZero (calendar. get (Calendar. HOUR_OF_DAY), 2 )). append (":"); // 14 buf. append (this. addZero (calendar. get (Calendar. MINUTE), 2 )). append (":"); 15 buf. append (this. addZero (calendar. get (Calendar. SECOND), 2 )). append (". "); 16 buf. append (this. addZero (calendar. get (Calendar. MILLISECOND), 3); 17 return buf. toString (); 18} 19 public String getDateComplete () {// get a date: format: MM minute ss second SSS millisecond 20 At HH on mm dd, yyyy // considering that the program needs to frequently modify the string, use StringBuffer to improve performance 21 StringBuffer buf = new StringBuffer (); 22 buf. append (calendar. get (Calendar. YEAR )). append ("year"); // increase by 23 buf. append (this. addZero (calendar. get (Calendar. MONTH) + 1, 2 )). append ("month"); // increase monthly buf. append (this. addZero (calendar. get (Calendar. DAY_OF_MONTH), 2 )). append ("day"); // get the 25 buf. append (this. addZero (calendar. get (Calendar. HOUR_OF_DAY), 2 )). append ("Hour"); // 26 buf. append (this. addZero (calendar. get (Calendar. MINUTE), 2 )). append ("points"); // score 27 buf. append (this. addZero (calendar. get (Calendar. SECOND), 2 )). append ("second"); // get second 28 buf. append (this. addZero (calendar. get (Calendar. MILLISECOND), 3 )). append ("millisecond"); // get millisecond 29 return buf. toString (); 30} 31 public String getTimeStamp () {// get a timestamp 32 // considering that the program needs to modify the String frequently, so use StringBuffer to improve performance 33 StringBuffer buf = new StringBuffer (); 34 buf. append (calendar. get (Calendar. YEAR); // increase by 35 buf. append (this. addZero (calendar. get (Calendar. MONTH) + 1, 2); // increase by 36 buckets per MONTH. append (this. addZero (calendar. get (Calendar. DAY_OF_MONTH), 2); // get the 37 buf. append (this. addZero (calendar. get (Calendar. HOUR_OF_DAY), 2); // 38 buf. append (this. addZero (calendar. get (Calendar. MINUTE), 2); // scored 39 buf. append (this. addZero (calendar. get (Calendar. SECOND), 2); // get the SECOND 40 buf. append (this. addZero (calendar. get (Calendar. MILLISECOND), 3); // get 41 return buf in milliseconds. toString (); 42} 43 // considering that there is a leading 0 in the date, add the method 44 private String addZero (int num, int len) with zeros here) {45 StringBuffer s = new StringBuffer (); 46 s. append (num); 47 while (s. length () <len) {// If the length is insufficient, add 048 s. insert (0, "0"); // Add 049} 50 return s at the first position. toString (); 51} 52}; 53 public class DateDemo06 {54 public static void main (String args []) {55 DateTime dt = new DateTime (); 56 System. out. println ("system date:" + dt. getDate (); 57 System. out. println ("Chinese Date:" + dt. getDateComplete (); 58 System. out. println ("timestamp:" + dt. getTimeStamp (); 59} 60 };

You can remember and use it frequently.

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.