租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

java如何获取当前时间戳,java如何获取当前日期和时间

发布时间:2023-10-24 11:53:59

java如何获得当前时间戳

在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