当前时间
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:1686122941014
timeString:2023-06-07 15:29:01.014
timeLong:1686122941014