Console.log("使用System.currentTimeMillis()獲取毫秒的時間戳"); Console.log(System.currentTimeMillis()); Console.log("使用Instant獲取秒的時間戳"); long unixTime = Instant.now().getEpochSecond(); Console.log(unixTime); System.out.println("將時間戳轉為日期"); Instant instant = Instant.ofEpochSecond(unixTime); Console.log(instant); System.out.println("使用Date獲取時間戳"); Date date = new Date(); long unixTime2 = date.getTime() / 1000L; Console.log(unixTime2);
輸出:
使用System.currentTimeMillis()獲取毫秒的時間戳
1611902068332
使用Instant獲取秒的時間戳
1611902068
將時間戳轉為日期
2021-01-29T06:34:28Z
使用Date獲取時間戳
1611902068