Java Timestamp: Get Current Time String

In Java, you can use the SimpleDateFormat class to obtain a timestamp string. The following code can be used to get the timestamp string of the current time:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        // 创建一个SimpleDateFormat对象,指定时间戳的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

        // 获取当前时间
        Date date = new Date();

        // 将当前时间格式化为时间戳字符串
        String timestamp = sdf.format(date);

        // 输出时间戳字符串
        System.out.println(timestamp);
    }
}

This will output a timestamp string like “20220101123456789” representing the current year, month, day, hour, minute, second, and millisecond.

bannerAds