방법
- System.currentTimeMillis();
public class GetCurrentTimestamp { public static void main(String[] args) { long currentTimeMillis = System.currentTimeMillis(); System.out.println("Current Timestamp: " + currentTimeMillis + " milliseconds"); } }
- java.util.Date()
import java.util.Date; public class GetCurrentTimestamp { public static void main(String[] args) { Date currentDate = new Date(); System.out.println("Current Timestamp: " + currentDate.getTime() + " milliseconds"); } }
- java.time.Instant
import java.time.Instant; public class GetCurrentTimestamp { public static void main(String[] args) { Instant currentInstant = Instant.now(); System.out.println("Current Timestamp: " + currentInstant.toEpochMilli() + " milliseconds"); } }
- java.time.LocalDateTime and java.time.ZoneId
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class GetCurrentTimestamp { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId); long currentTimestamp = zonedDateTime.toInstant().toEpochMilli(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = localDateTime.format(formatter); System.out.println("Current Timestamp: " + currentTimestamp + " milliseconds"); System.out.println("Formatted Date-Time: " + formattedDateTime); } }
- java.sql.Timestamp
public class GetCurrentTimestamp { public static void main(String[] args) { Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis()); System.out.println("Current Timestamp: " + currentTimestamp.getTime() + " milliseconds"); System.out.println("Formatted Timestamp: " + currentTimestamp.toString()); } }