Java Basics-String, Date of a common class

Source: Internet
Author: User
Tags control characters dateformat

--------------------------------------------String Class--------------------------------------------

Main content: String, StringBuffer, StringBuilder, String.Format ()

StringBuffer is thread-safe, StringBuilder is not, but StringBuilder efficient, so more commonly

StringBuilder function is the same as Stringbufer

StringBuilder vs=new StringBuilder ();

Vs.append (7). Append ("a dwarf"); 7 Little dwarves.

Vs.delete (0,2); Little Dwarf

Vs.reverse ();//Human dwarf

Vs.delete (0,vs.length ()); Empty

Vs.tostring ();//Convert to immutable string

String.Format () returns a string that functions like printf ()

Format String.Format ("[Information]< Format Control >", data list);

Format Control characters:

%d decimal

%f floating point String.Format ("%8.2f", 123456.789); 123456.78

%c character

%s string

N.M indicates that integers and decimals account for n decimal places

0 Vacant String.Format ("%012.2f", 123456.789); 000123456.78

-Align Left

n$ represents the nth data String.Format ("%1$8.2", 123456.789); 123456.78

%TF Date

%tt time String.Format ("%tt", new Date ()); 20:25:32

%ta Week

%ty year

%tm Month

--------------------------------------------the Date class-------------------------------------------

Main content: Date, DateFormat, SimpleDateFormat

Date D1=new date ();

Date D2=new date ();

Boolean flg=flase;

Long T1=d1.gettime ();//Get a Long value

Flg=d1.equals (D2);//Whether the time is the same

int X=d1.compareto (D2);//D1 is behind D2

Flg=d1.after (D2);//Ibid.

Flg=d1.before (D2);//d1 before D2

Extension: Date d3=new date (365l*24*60*60*1000); int to long put L on the front

Formatted Date (date→string)

Date D=new date ();

String str1=dateformat.getdateinstance (). Format (d); 2016-05-05

String str2=dateformat.gettimeinstance (). Format (d);//20:49:50

String str3=dateformat.getdatetimeinstance (). Format (d);//2016-05-05 20:49:50

Custom format, attention to exception handling (string→date)

Str= "2016/05/05";

D=dateformat.getdateinstace (). Parse (str1);//The format must be YYYY-MM-DD, otherwise the exception

D=new SimpleDateFormat ("Yyyy+mm+dd"). Parse (str),//STR format is not fixed, even can be "2016 Ah March Oh 2nd ah ..."

Extended:

Scanner sc=new Scanner (system.in);

String S1=sc.next (); Space

String S2=sc.nextline (); Enter

Java Basics-String, Date of a common class

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.