Java中时间戳的使用
当前时间
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
学习更多编程知识,请关注我的公众号:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-06-14 Git的使用