Java Common Code Summary

Source: Internet
Author: User

Original works, can be reproduced, but please mark the source address: http://www.cnblogs.com/V1haoge/p/7004474.html

1. Conversion between dates and strings

1      Public Static voidMain (string[] args) {2Date now =NewDate ();3String d = "2017-06-13 23:23:23";4         5System.out.println (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). format (now));6         Try {7System.out.println (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Parse (d));8}Catch(ParseException e) {9 e.printstacktrace ();Ten         } One}

Results:

2017-06-13 23:31:1223:23:23 gmt+08:00 2017

2. Numeric string formatting

1      Public Static void Main (string[] args) {2         Double d = 1234567890.5512d; 3         System.out.println (String.Format ("%,.2f", D)); 4     }

Results:

1,234,567,890.55

3. Date string formatting

1      Public Static voidMain (string[] args) {2Date now =NewDate ();3System.out.println (String.Format ("%TC", now));4System.out.println (String.Format ("%TF", now));5System.out.println (String.Format ("%TD", now));6System.out.println (String.Format ("%tr", now));7System.out.println (String.Format ("%tt", now));8System.out.println (String.Format ("%tr", now));9System.out.println (String.Format ("%1$TF%2$tt", Now,now));TenSystem.out.printf ("%tc%n", now); OneSystem.out.printf ("%tt%n", now); ASystem.out.printf ("%1 $ s%2$TB%2$te,%2$ty%n", "Date:", now); -System.out.printf ("%1$TF%2$tt", Now,now); -}

Results:

Monday 36 month 01:29:40 gmt+08:00 20172017-06-1406/14/1701:29:40 morning 01:29:4001:292017-06-14 01:29:4014 01:29:40 gmt+08:00 201701:29:4014,20172017-06-14 01:29:40

Java Common Code Summary

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.