Java SE Basic section--SimpleDateFormat of common class libraries (date formatting)

Source: Internet
Author: User

Gets the current date and formats the input according to different dates. The code is as follows:

1 //20160618 Use date formatting exercises for Simpledatefomat classes2  PackageMyPackage;//a package of your own definition3 4 ImportJava.text.SimpleDateFormat;//Import the required SimpleDateFormat package5 ImportJava.util.Date;//Import the required date package6 7 classMydatedemo {//the Mydatedemo class defined8     PrivateSimpleDateFormat SD =NULL;//declaring SimpleDateFormat object SD9 Ten      PublicString getDate01 () {//defining the GetDate01 method One          This. SD =NewSimpleDateFormat ("Yyyy-mm-dd hh:mm;ss.sss");//get a "yyyy-mm-dd A                                                                     //hh:mm;ss.sss "Format Date -         return  This. Sd.format (NewDate ());//Format the current date -     } the  -      PublicString GetDate02 () {//defining the GetDate02 method -          This. SD =NewSimpleDateFormat ("yyyy mm month DD day hh" mm min ss sec sss milliseconds ");//get a "yyyy year mm DD Day" -                                                                         //hh mm min ss sec sss milliseconds format Date +         return  This. Sd.format (NewDate ());//Format the current date -     } +  A      PublicString getDate03 () {//defining the GetDate03 method at          This. SD =NewSimpleDateFormat ("Yyyymmddhhmmsssss");//get a "yyyymmddhhmmsssss" format date (i.e. timestamp) -         return  This. Sd.format (NewDate ());//Format the current date -     } - } -  -  Public classSimpledateformatdemo {//Main class in  -      Public Static voidMain (string[] args) {//Main Method toMydatedemo DD =NewMydatedemo ();//declares a DD object and instantiates +SYSTEM.OUT.PRINTLN ("Default date format:" +NewDate ());//call methods to enter dates in different formats, respectively -System.out.println ("English date format:" +dd.getdate01 ()); theSystem.out.println ("Chinese date format:" +dd.getdate02 ()); *System.out.println ("Timestamp:" +dd.getdate03 ()); $ Panax Notoginseng     } -  the}

there is another way to format the date, this method takes precedence, and the other method code is as follows:

1 Importjava.util.*;//Import the required toolkit2 classdatetime{//Date time can be obtained directly from this class in the future3     PrivateCalendar Calendar =NULL;//declare a Calendar object, get time4      PublicDateTime () {//directly instantiating an object in a construction method5          This. Calendar =NewGregorianCalendar (); 6     }7      PublicString GetDate () {//Get a Date: The format is: Yyyy-mm-dd HH:mm:ss. SSS8         //use StringBuffer to improve performance, given that the program frequently modifies strings9StringBuffer buf =NewStringBuffer ();TenBuf.append (Calendar.get (Calendar.year)). Append ("-");//Increased year OneBuf.append ( This. Addzero (Calendar.get (calendar.month) +1,2)). Append ("-");//Increase Month ABuf.append ( This. Addzero (Calendar.get (Calendar.day_of_month), 2)). Append ("");//Acquisition Date -Buf.append ( This. Addzero (Calendar.get (Calendar.hour_of_day), 2)). Append (":");//when acquired -Buf.append ( This. Addzero (Calendar.get (Calendar.minute), 2)). Append (":") ; theBuf.append ( This. Addzero (Calendar.get (Calendar.second), 2)). Append (".") ; -Buf.append ( This. Addzero (Calendar.get (Calendar.millisecond), 3)) ; -         returnbuf.tostring (); -     } +      PublicString Getdatecomplete () {//get a Date: format: yyyy mm month dd Day hh mm min SS sec sss ms -         //use StringBuffer to improve performance, given that the program frequently modifies strings +StringBuffer buf =NewStringBuffer (); ABuf.append (Calendar.get (Calendar.year)). Append ("year");//Increased year atBuf.append ( This. Addzero (Calendar.get (calendar.month) +1,2)). Append ("month");//Increase Month -Buf.append ( This. Addzero (Calendar.get (Calendar.day_of_month), 2)). Append ("Day");//Acquisition Date -Buf.append ( This. Addzero (Calendar.get (Calendar.hour_of_day), 2)). Append ("Time");//when acquired -Buf.append ( This. Addzero (Calendar.get (Calendar.minute), 2)). Append ("min");//Get points -Buf.append ( This. Addzero (Calendar.get (Calendar.second), 2)). Append ("seconds");//Get seconds -Buf.append ( This. Addzero (Calendar.get (Calendar.millisecond), 3)). Append ("MS");//get milliseconds in         returnbuf.tostring (); -     } to      PublicString Gettimestamp () {//got a time stamp +         //use StringBuffer to improve performance, given that the program frequently modifies strings -StringBuffer buf =NewStringBuffer (); theBuf.append (Calendar.get (calendar.year));//Increased year *Buf.append ( This. Addzero (Calendar.get (calendar.month) +1,2));//Increase Month $Buf.append ( This. Addzero (Calendar.get (Calendar.day_of_month), 2));//Acquisition DatePanax NotoginsengBuf.append ( This. Addzero (Calendar.get (Calendar.hour_of_day), 2));//when acquired -Buf.append ( This. Addzero (Calendar.get (Calendar.minute), 2));//Get points theBuf.append ( This. Addzero (Calendar.get (Calendar.second), 2));//Get seconds +Buf.append ( This. Addzero (Calendar.get (Calendar.millisecond), 3));//get milliseconds A         returnbuf.tostring (); the     } +     //Given that there is a leading 0 in the date, the method to add a zero complement here -     PrivateString Addzero (intNumintLen) { $StringBuffer s =NewStringBuffer (); $ s.append (num); -          while(S.length () <len) {//if the length is insufficient, continue to fill 0 -S.insert (0, "0");//fill 0 at the first position the         } -         returns.tostring ();Wuyi     } the }; -  Public classdatedemo06{ Wu      Public Static voidMain (String args[]) { -DateTime dt =NewDateTime (); AboutSYSTEM.OUT.PRINTLN ("System date:" +dt.getdate ()); $System.out.println ("Chinese Date:" +Dt.getdatecomplete ()); -System.out.println ("Timestamp:" +Dt.gettimestamp ()); -     } -};

Often used, it can be skilled to remember and use.

Java SE Basic section--SimpleDateFormat of common class libraries (date formatting)

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.