Java日期时间字符串和毫秒相互转换的方法
作者:Silent_Paladin 时间:2022-03-11 18:15:57
本文内容大多基于官方文档和网上前辈经验总结,经过个人实践加以整理积累,仅供参考。
1、日期时间字符串转换成毫秒
@Test
public void test() throws ParseException {
String dateTime = "2016-12-31 12:30:45 123";
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").parse(dateTime));
System.out.println("日期[2016-12-31 12:30:45 123]对应毫秒:" + calendar.getTimeInMillis());
}
运行结果:
2、毫秒转换成日期时间字符串
@Test
public void test() {
long millisecond = 1483159625851l;
Date date = new Date(millisecond);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss SSS a");
System.out.println("毫秒[1483159625851]对应日期时间字符串:" + format.format(date));
}
运行结果:
更多日期时间字符串格式请参考:Java:SimpleDateFormat
来源:http://blog.csdn.net/Silent_Paladin/article/details/54647438
标签:Java,日期,时间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android仿简书搜索框效果的示例代码
2023-06-18 16:02:58
![](https://img.aspxhome.com/file/2023/7/83527_0s.gif)
Spring Boot2.0中SpringWebContext找不到无法使用的解决方法
2023-05-26 02:37:01
![](https://img.aspxhome.com/file/2023/1/72661_0s.jpg)
java实现简单石头剪刀布游戏
2023-07-20 05:31:37
![](https://img.aspxhome.com/file/2023/8/57628_0s.jpg)
基于ReentrantLock的实现原理讲解
2023-11-23 22:43:23
![](https://img.aspxhome.com/file/2023/9/59849_0s.jpg)
C#求n个数中最大值和最小值的方法
2022-04-20 08:00:01
C++实现LeetCode(131.拆分回文串)
2023-07-24 09:58:42
spring框架cacheAnnotation缓存注释声明解析
2022-04-14 17:13:05
![](https://img.aspxhome.com/file/2023/3/65323_0s.png)
SpringBoot+Redis实现数据字典的方法
2022-08-03 14:22:29
![](https://img.aspxhome.com/file/2023/4/65364_0s.jpg)
mybatis报错元素内容必须由格式正确的字符数据或标记组成异常的解决办法
2023-01-10 15:46:37
深入理解Java设计模式之装饰模式
2021-11-23 11:29:52
![](https://img.aspxhome.com/file/2023/3/88153_0s.jpg)
快速了解hibernate配置文件与映射文件
2023-11-04 23:02:26
java9新特性Collection集合类的增强与优化方法示例
2023-01-29 05:16:57
![](https://img.aspxhome.com/file/2023/4/61534_0s.png)
一文搞懂Spring循环依赖的原理
2023-07-24 19:03:24
Java 数据结构中二叉树前中后序遍历非递归的具体实现详解
2023-02-14 12:51:18
![](https://img.aspxhome.com/file/2023/7/75757_0s.png)
你知道将Bean交给Spring容器管理有几种方式(推荐)
2021-07-17 08:36:23
![](https://img.aspxhome.com/file/2023/7/63447_0s.png)
Androd 勇闯高阶性能优化之布局优化篇
2023-01-22 12:12:15
![](https://img.aspxhome.com/file/2023/8/87278_0s.png)
详解Java使用Pipeline对Redis批量读写(hmset&hgetall)
2023-11-17 15:17:24
Java 重写时应当遵守的 11 条规则
2023-02-10 18:58:06
![](https://img.aspxhome.com/file/2023/9/70999_0s.png)
C#预定义的基础类型转换
2023-08-13 03:14:16
![](https://img.aspxhome.com/file/2023/8/69788_0s.png)
android电源信息查看(电量、温度、电压)实例代码
2022-04-30 15:41:04