Java常用代碼總結

來源:互聯網
上載者:User

標籤:div   ble   之間   nts   ati   代碼   rgs   back   ring   

原創作品,可以轉載,但是請標註出處地址:http://www.cnblogs.com/V1haoge/p/7004474.html 

1、日期與字串之間的轉換

 1     public static void main(String[] args) { 2         Date now = new Date(); 3         String d = "2017-06-13 23:23:23"; 4          5         System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now)); 6         try { 7             System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d)); 8         } catch (ParseException e) { 9             e.printStackTrace();10         }11     }

結果:

2017-06-13 23:31:12Tue Jun 13 23:23:23 GMT+08:00 2017

2、數值字串格式化

1     public static void main(String[] args) {2         Double d = 1234567890.5512d;3         System.out.println(String.format("%,.2f", d));4     }

結果:

1,234,567,890.55

3、日期文字格式化

 1     public static void main(String[] args) { 2         Date now = new Date(); 3         System.out.println(String.format("%tc", now)); 4         System.out.println(String.format("%tF", now)); 5         System.out.println(String.format("%tD", now)); 6         System.out.println(String.format("%tr", now)); 7         System.out.println(String.format("%tT", now)); 8         System.out.println(String.format("%tR", now)); 9         System.out.println(String.format("%1$tF %2$tT", now,now));10         System.out.printf("%tc%n",now);11         System.out.printf("%tT%n",now);12         System.out.printf("%1$s %2$tb %2$te,%2$tY%n","date:",now);13         System.out.printf("%1$tF %2$tT",now,now);14     }

結果:

星期三 六月 14 01:29:40 GMT+08:00 20172017-06-1406/14/1701:29:40 上午01:29:4001:292017-06-14 01:29:40星期三 六月 14 01:29:40 GMT+08:00 201701:29:40date: 六月 14,20172017-06-14 01:29:40

 

Java常用代碼總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.