java获取时间戳有多少种方式,给我写出5种以上示例

您所在的位置:网站首页 5分钟时间戳是多少 java获取时间戳有多少种方式,给我写出5种以上示例

java获取时间戳有多少种方式,给我写出5种以上示例

#java获取时间戳有多少种方式,给我写出5种以上示例| 来源: 网络整理| 查看: 265

Java中获取时间戳的方式有很多种, 以下是几种常用的方法:

使用System.currentTimeMillis()方法: long timestamp= System.currentTimeMillis(); 使用Instant.now().toEpochMilli()方法: import java.time.Instant; long timestamp = Instant.now().toEpochMilli(); 使用new Date().getTime()方法: import java.util.Date; long timestamp = new Date().getTime(); 使用Calendar.getInstance().getTimeInMillis()方法: import java.util.Calendar; long timestamp = Calendar.getInstance().getTimeInMillis(); 使用LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() 方法: import java.time.LocalDateTime; import java.time.ZoneOffset; long timestamp = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); 使用 ZonedDateTime.now(ZoneId.systemDefault()).toInstant().toEpochMilli() 方法:import java.time.ZonedDateTime; import java.time.ZoneId; long timestamp = ZonedDateTime.now(ZoneId.systemDefault()).toInstant().toEpochMilli();

以上几种方法均可获取当前时间的时间戳, 不同之处在于使用的类和方法不同。

注意: 上面第5种和第6种和前面不同之处是 使用了带时区的时间,可以自己设置时区取得时间戳。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3