扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

java生成时间戳的方法有哪些

扬州沐宇科技
2023-11-16 19:29:56
Java

Java中生成时间戳的方法有以下几种:

  1. 使用System.currentTimeMillis()方法: long timestamp = System.currentTimeMillis();

  2. 使用Date类: Date date = new Date(); long timestamp = date.getTime();

  3. 使用Calendar类: Calendar calendar = Calendar.getInstance(); long timestamp = calendar.getTimeInMillis();

  4. 使用Instant类(Java 8及以上版本): Instant instant = Instant.now(); long timestamp = instant.toEpochMilli();

以上方法都可以获得当前时间的毫秒级时间戳。

扫码添加客服微信