Java Date class and SimpleDateFormat dates class format

Source: Internet
Author: User
Tags dateformat locale string format

~date represents a specific time, accurate to milliseconds
~ Construction Method:
Public date ()//time when the date object was constructed and initialized to the current system
Public date (long date)//1970-1-1 0:0:0 the number of milliseconds to the specified time

~ Common methods:
Public long GetTime ()//1970-1-1 0:0:0 to the current number of milliseconds
Public long settime ()//Set Date Time
public boolean befor (date time)//test whether this date is before the specified date
public Boolean after (date time)//test whether this date is after the specified date
public int CompareTo (date anotherdate)//assumes that the current date is before the date parameter, returns <0; the current date after the date parameter, and returns >0
public string toString ()//Convert date format to string format output

~dateformat is a date/time formatted abstract class that formats and parses dates or times in a language-neutral way
~ Date/Time formatting subclasses (such as SimpleDateFormat) allow formatting (that is, date-and text), parsing (text--date)
~ Construction Method:
Public Simlpedateformat ()
Public SimpleDateFormat (String pattern)
~ Common methods:
Public final string format (date date)//date to String
Public Date Parse (string source)//string converted to date

ImportJava.text.DateFormat;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Locale; Public classDatedemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubDate Date =NewDate ();        SYSTEM.OUT.PRINTLN (date); System.out.println (Date.gettime ());//returns the number of milliseconds from 1970.1.1 to the presentDate.settime (1519807159999L);//Modification TimeSystem.out.println (date); DateFormat DF1=NULL;//DateFormat is not instantiated for abstract classesDateFormat DF2 =NULL;//Ibid .DF1 = Dateformat.getdateinstance ();//implement subclass object, get dateDF2 = Dateformat.getdatetimeinstance ();//Get date + timeSystem.out.println ("Date:" +df1.format (date));//converts a string to a date format, fixedSystem.out.println ("DateTime:" +df2.format (date));//Convert a string to date + time format, format fixedDateFormat df3=NULL; DF3= Dateformat.getdateinstance (Dateformat.full,NewLocale ("zh", "CN"));//format to Chinese date mode. There are four formats of Short,medium,long,fullSystem.out.println ("Date:" +df3.format (date));//Convert a string to date + time format, format fixedDateFormat DF4 =NULL; DF4= Dateformat.getdatetimeinstance (Dateformat.full,dateformat.full,NewLocale ("zh", "CN")); System.out.println ("DateTime:" +df4.format (date));//Convert a string to date + time format, format fixed//SimpleDateFormat Subclasses can customize the output format for more flexibilityString strdate = "2018-2-29 18:30:00.123"; Date D=NULL; SimpleDateFormat SDF1=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. SSS "); SimpleDateFormat SDF2=NewSimpleDateFormat ("yyyy mm month DD Day Hh-mm-ss.sss"); Try{//If there is a format to write an exception, throw the exceptionD =Sdf1.parse (strdate); }Catch(Exception e) {} System.out.println (d); String Str= Sdf2.format (d);//output the date in the specified formatSystem.out.println (str); }}

Java Date class and SimpleDateFormat date class format

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.