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:
- The current system time in milliseconds.
long timestamp = System.currentTimeMillis();
System.out.println("当前时间戳:" + timestamp);
- a class in Java called Date
- 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.