温馨提示×

java怎么获取时间戳字符串

小亿
96
2024-02-05 16:44:36
栏目: 编程语言

在Java中,可以使用SimpleDateFormat类来获取时间戳字符串。可以使用以下代码来获取当前时间的时间戳字符串:

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);
    }
}

这将输出一个形如"20220101123456789"的时间戳字符串,表示当前时间的年、月、日、时、分、秒和毫秒。

0