原文链接

当前时间

import java.sql.Timestamp; //导包Timestamp nowTime = new Timestamp(System.currentTimeMillis());System.out.println(nowTime);

输出:

2022-06-08 11:15:51.014

Long型时间戳

Long timeLong = System.currentTimeMillis();System.out.println("timeLong:" + timeLong + "\n");

输出:

1652862076686

时间戳和时间的转化

public void learnCron() throws ParseException {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // 设置格式Long currentTime = System.currentTimeMillis();// 获取当前时间戳System.out.println("currentTime:" + currentTime);String timeString = format.format(currentTime);// 时间戳转为时间格式System.out.println("timeString:" + timeString);Date date = format.parse(timeString); // 时间格式转为时间戳long timeLong = date.getTime();System.out.println("timeLong:" + timeLong);}

输出:

currentTime:1686122941014timeString:2023-06-07 15:29:01.014timeLong:1686122941014

学习更多编程知识,请关注我的公众号:

代码的路