暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

Java中时间戳的使用

原创 代码的路 2023-06-14
314

原文链接

当前时间

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

 
 

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

代码的路

 
 

最后修改时间:2023-06-15 16:44:29
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论