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

 
 

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

代码的路

 
 

posted @   代码的路  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2022-06-14 Git的使用
点击右上角即可分享
微信分享提示