java怎么获取当前时间戳
导读:在Java中,可以使用System.currentTimeMillis( 方法获取当前时间戳,返回的是从1970年1月1日00:00:00 GMT到当前时间的毫秒数。另外,可以使用Instant.now( .toEpochMilli( 方法...
在Java中,可以使用System.currentTimeMillis()方法获取当前时间戳,返回的是从1970年1月1日00:00:00 GMT到当前时间的毫秒数。另外,可以使用Instant.now().toEpochMilli()方法也可以获取当前时间戳。以下是示例代码:
public class Main {
public static void main(String[] args) {
// 使用System.currentTimeMillis()获取当前时间戳
long timestamp1 = System.currentTimeMillis();
System.out.println("当前时间戳1:" + timestamp1);
// 使用Instant.now().toEpochMilli()获取当前时间戳
long timestamp2 = Instant.now().toEpochMilli();
System.out.println("当前时间戳2:" + timestamp2);
}
}
输出结果类似于:
当前时间戳1:1627047058000
当前时间戳2:1627047058000
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: java怎么获取当前时间戳
本文地址: https://pptw.com/jishu/567480.html
