Java之時間轉換

來源:互聯網
上載者:User

標籤:時間類型   form   atd   IV   long   tac   exception   表示   mss   

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    Date date = sdf.parse("2018-06-05 13:01:25");        System.out.println(date.getTime());    System.out.println(sdf.format(date.getTime()));   

    這個例子就足以說明Date類型的資料如何轉換為Long類型
    Long類型的日期如何轉換為Date
    
    特別是第三方介面,很多介面傳參除了傳簽名外還有時間戳記,時間戳記通常就是用Long類型的時間表示的(這裡指的是將Date轉為Long類型)
    之所以做主要考慮到安全。

     Date dt =new Date();          System.out.println(dt); //格式: Wed Jul 06 09:28:19 CST 2016                    //格式:2018-6-24          String formatDate = null;          formatDate = DateFormat.getDateInstance().format(dt);          System.out.println(formatDate);                      //格式:2018年6月24日 星期三          formatDate = DateFormat.getDateInstance(DateFormat.FULL).format(dt);          System.out.println(formatDate);                    //格式 24小時制:2018-06-24 09:39:58          DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小時制;          formatDate = dFormat.format(dt);          System.out.println(formatDate);                    //格式12小時制:2018-06-24 09:42:44          DateFormat dFormat12 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //hh表示12小時制;          formatDate = dFormat12.format(dt);          System.out.println(formatDate);                    //格式去掉分隔字元24小時制:20160706094533          DateFormat dFormat3 = new SimpleDateFormat("yyyyMMddHHmmss");          formatDate = dFormat3.format(dt);          System.out.println(formatDate);                    //格式轉成long型:1467770970          long lTime = dt.getTime() / 1000;          System.out.println(lTime);                    //格式long型轉成Date型,再轉成String:  1464710394 -> ltime2*1000 -> 2018-06-24 23:59:54          long ltime2 = 1464710394;          SimpleDateFormat lsdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          Date lDate = new Date(ltime2*1000);          String lStrDate = lsdFormat.format(lDate);          System.out.println(lStrDate);                    //格式String型轉成Date型:2018-06-24 10:17:48 -> Wed Jul 06 10:17:48 CST 2016          String strDate = "2018-06-24 10:17:48";          SimpleDateFormat lsdStrFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          try {              Date strD = lsdStrFormat.parse(strDate);              System.out.println(strD);          } catch (ParseException e) {              e.printStackTrace();          }  

 以上是比較常用的時間類型轉換,通常簽名比較常用的就是Long類型的時間戳記,支付用的比較多,為了安全起見。

至於String類型的話,什麼建立時間,更新時間等比較常用。

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.