java把13位時間戳轉換成"yyyy-MM-dd HH:mm:ss"格式的工具類
阿新 • • 發佈:2019-01-31
package com.liming.test; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.junit.Test; public class Test4 { @Test public void testConn() throws SQLException{ String time = "1520423832415"; String timeStamp2Date = timeStamp2Date(time); System.out.println(timeStamp2Date); } public static String timeStamp2Date(String time) { Long timeLong = Long.parseLong(time); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要轉換的時間格式 Date date; try { date = sdf.parse(sdf.format(timeLong)); return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } }