java把13位时间戳转换成"yyyy-MM-dd HH:mm:ss"格式,工具类

 

 

public static void main(String[] args) {

    String time = System.currentTimeMillis();//获取当前时间精确到毫秒级的时间戳,例:1525849325942

    System.out.println(timeStamp2Date(time))

}

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 (ParseException e) {
        e.printStackTrace();
        return null;
    }
}                

 

posted @ 2018-05-09 15:06  宇枫  阅读(28710)  评论(0编辑  收藏  举报