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