获取当前时间戳的方法有很多种,可以根据你的需求和使用的Java版本来选择适合的方法。以下是五种获取当前时间戳的方法:
System.currentTimeMillis()long currentTimeMillis = System.currentTimeMillis(); java.util.DateDate currentDate = new Date(); long timestamp = currentDate.getTime(); java.time.InstantInstant currentInstant = Instant.now(); long timestamp = currentInstant.toEpochMilli(); java.time.LocalDateTime和java.time.ZoneIdLocalDateTime localDateTime = LocalDateTime.now(); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId); long currentTimestamp = zonedDateTime.toInstant().toEpochMilli(); java.sql.TimestampTimestamp currentTimestamp = new Timestamp(System.currentTimeMillis()); long timestamp = currentTimestamp.getTime(); 根据你的具体需求,选择其中一种方法即可获取当前时间的时间戳。
最常用的是方法1 System.currentTimeMillis()