Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转
本文收录在猪哥GitHub:https://github.com/pig6/Java 中,本项目收集一线大厂面试、实战、Java学习路线等。
本文目前提供:LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转
文中都使用的时区都是东8区,也就是北京时间。这是为了防止服务器设置时区错误时导致时间不对,如果您是其他时区,请自行修改
1.LocalDateTime获取毫秒数
//获取秒数
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
//获取毫秒数
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
2.LocalDateTime与String互转
//时间转字符串格式化DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);//字符串转时间
String dateTimeStr = "2018-07-28 14:11:15";
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);
3.Date与LocalDateTime互转
//将java.util.Date 转换为java8 的java.time.LocalDateTime,默认时区为东8区public static LocalDateTime dateConvertToLocalDateTime(Date date) {return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();}//将java8 的 java.time.LocalDateTime 转换为 java.util.Date,默认时区为东8区public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));}/*** 测试转换是否正确*/@Testpublic void testDateConvertToLocalDateTime() {Date date = DateUtils.parseDate("2018-08-01 21:22:22", DateUtils.DATE_YMDHMS);LocalDateTime localDateTime = DateUtils.dateConvertToLocalDateTime(date);Long localDateTimeSecond = localDateTime.toEpochSecond(ZoneOffset.of("+8"));Long dateSecond = date.toInstant().atOffset(ZoneOffset.of("+8")).toEpochSecond();Assert.assertTrue(dateSecond.equals(localDateTimeSecond));}
更多Java优质文章,请关注猪哥微信公众号:猪哥Java!
Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转相关推荐
- QT获取时间戳毫秒和秒的方法
获取时间戳毫秒: QDateTime::currentDateTime().toTime_t() * 1000; 或 QDateTime dateTime = QDateTime::currentDa ...
- LocalDateTime获取时间戳、LocalDateTime与String互转、Date与LocalDateTime互转(Java8 特性)
LocalDateTime与String.Date的互转 废话不说了,直接上代码,喜欢自取:可以封装成util类~~ 1.得到当前的localDateTime public static LocalD ...
- Node.JS 获取时间戳(毫秒,秒)
/*** 获取当前时间戳(毫秒)*/ function getNowMilliSecond(){return Math.floor(Date.now()); }/*** 获取当前时间戳(秒)*/ fu ...
- python获取时间戳毫秒级_Python获取秒级时间戳与毫秒级时间戳
1.获取秒级时间戳与毫秒级时间戳.微秒级时间戳 import time import datetime t = time.time() print (t) #原始时间数据 print (int(t)) ...
- 原生JS实现拿到的时间戳“毫秒/秒”转为“时:分:秒”形式的效果
在日常编程中,我们向后台数据,请求的可能是视频或者音乐的接口,后端返回给我们的数据中可能是视频或音乐的时长,通常都会给我们返回一个时间戳形式的字符.比如1454646毫秒或者秒.这个时候我们就需要把这 ...
- LocalDateTime 与时间戳相互转换
/*** LocalDateTime 转 时间戳(秒级别)** @param localDateTime* @return Long* @author Caixiaowei* @updateTime ...
- php上个月,PHP获取上个月、下个月、本月的日期(strtotime(),date())
PHP获取上个月.下个月.本月的日期(strtotime(),date()) 來源:互聯網 2016-05-25 17:20:41 評論 php获取上个月.下个月.本月的日期(strtotime( ...
- Java8 LocalDateTime 获取当前日周月年的起止时间
Java8 LocalDateTime 常见使用_获取当天.本周.本月.本季度.本年度起止时间 一.返回 LocalDateTime 格式的起止时间 二.返回 String 格式的起止时间 一.返回 ...
- LocalDateTime获取当前时间戳
以上都是获取当前时间的时间戳.而要想使用localdateTime获取当前时间,只需要LocalDateTime.now()即可. 输出结果如下:
最新文章
- 干货丨深度学习、图像分类入门,从VGG16卷积神经网络开始
- visual assist x太卡了_LeetCode69. x 的平方根
- raspberry树莓派NFS搭建
- Git :LF will be replaced by CRLF in readme.txt的原因与解决方案
- 吴恩达深度学习笔记11-Course4-Week2【深度卷积网络:实例探究】
- python3.70_Python3 基本数据类型
- NOIP2003普及组第1题 乒乓球(Table.cpp)-- python3实现
- 主程序与子程序不在同一程序模块中_数控车床子程序M98、M99编程实例!
- 带有服务器回调的通用客户端——服务器 DotNet远程处理
- 获取点击按钮的元素_怎么按顺序自动点击网页所有链接
- 网站上传服务器及安装包,如何上传安装包到服务器
- LMDB:轻量级内存映射数据库-----入门使用1
- kafka的offset理解
- R语言中的并行计算实现
- SQL注入学习日记(一)
- 获取具体日期N个月前(后)的自然月时间戳
- evo_traj 错误:[ERROR] Unhandled error in evo.main_traj
- 研报复现系列(二):【光大证券】基于阻力支撑相对强度(RSRS)的市场择时
- 使用Weinre调试webapp
- lua 反汇编工具 ChunkSpy 的使用