How to write code in Java to get a timestamp.

There are multiple ways to get a timestamp in Java, here are two commonly used methods:

  1. The current system time in milliseconds.
long timestamp = System.currentTimeMillis();
System.out.println("当前时间戳:" + timestamp);
  1. a class in Java called Date
  2. Obtain the current time.
import java.util.Date;

Date date = new Date();
long timestamp = date.getTime();
System.out.println("当前时间戳:" + timestamp);

The above code will produce the timestamp of the current time.

bannerAds