java日期和時間戳格式互轉

// 將日期格式轉換成時間戳
1 public static void main(String[] args) throws Exception{
2     String time = "2018-05-12 12:30:59";
3     Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
4     Long time1 = parse.getTime();
5     System.out.println(time + " >> " + time1);
6 }

// 將時間戳轉換成日期格式
1 public static void main(String[] args) {
2     Long time  = 1527816283000L;
3     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
4     String time1 = sdf.format(new Date(time));
5     System.out.println(time + " >> " + time1);
6 }
相關文章
相關標籤/搜索