java 日期的格式化 輸入/輸出

來源:互聯網
上載者:User

標籤:blog   格式化   mat   main   ack   second   tin   div   stat   

想要得到形如2018.07.09的格式化好的當天日期

建立Date對象,調用SimpleDateFormat對象的format方法:

indexstr="logstash-"+new SimpleDateFormat("yyyy.MM.dd").format(new Date());


package com.ob;    import java.text.ParseException;  import java.text.SimpleDateFormat;  import java.util.Calendar;  import java.util.Date;    public class DateTest {        public static void main(String[] args) throws ParseException {          Calendar now = Calendar.getInstance();          System.out.println("年: " + now.get(Calendar.YEAR));          System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");          System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));          System.out.println("時: " + now.get(Calendar.HOUR_OF_DAY));          System.out.println("分: " + now.get(Calendar.MINUTE));          System.out.println("秒: " + now.get(Calendar.SECOND));          System.out.println("目前時間毫秒數:" + now.getTimeInMillis());          System.out.println(now.getTime());            Date d = new Date();          System.out.println(d);          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          String dateNowStr = sdf.format(d);          System.out.println("格式化後的日期:" + dateNowStr);                    String str = "2012-1-13 17:26:33";  //要跟上面sdf定義的格式一樣          Date today = sdf.parse(str);          System.out.println("字串轉成日期:" + today);      }  }  

輸出結果:

年: 2012
月: 1
日: 13
時: 17
分: 28
秒: 19
目前時間毫秒數:1326446899902
Fri Jan 13 17:28:19 CST 2012
Fri Jan 13 17:28:19 CST 2012
格式化後的日期:2012-01-13 17:28:19
字串轉成日期:Fri Jan 13 17:26:33 CST 2012




參考77482694

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.