Java将Unix时间戳转换成指定格式日期

时间:2024-01-15 15:44:44

public String TimeStamp2Date(String timestampString, String formats){  

  Long timestamp = Long.parseLong(timestampString)*1000;  

  String date = new java.text.SimpleDateFormat(formats).format(new java.util.Date(timestamp));  

  return date;

}

当调用TimeStampToDate("1252639886", "yyyy-MM-dd HH:mm:ss");

返回值:2009-11-09 11:31:26

注:此处传入必须为 Long,而非long  java.util.Date(Long timestamp)