首页 > 编程语言 > 详细

JAVA-日期处理-LocalDate-时区

时间:2021-05-08 22:30:57      阅读:14      评论:0      收藏:0      [点我收藏+]

 

三种时区表示方式,LocalDate转成字符串日期和ts的方法

LocalDateTime localDateTime = null;
//时区写法1:
String timezone = "Asia/Shanghai";
//###### 字符串时间转ts
ZoneId zoneId = ZoneId.of(timezone);
localDateTime = LocalDateTime.parse("2018-06-26 16:29:42", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
long milliSecond = localDateTime.atZone(zoneId).toInstant().toEpochMilli();
System.out.println("milliSecond:" + milliSecond);
//###### ts转字符串时间
Instant instant = Instant.ofEpochMilli(milliSecond);
String date = LocalDateTime.ofInstant(instant, zoneId).toString();
System.out.println("date:" + date);
/**********************************************************************************/
//时区写法2:
timezone = "GMT+8";
//###### 字符串时间转ts
zoneId = ZoneId.of(timezone);
localDateTime = LocalDateTime.parse("2018-06-26 16:29:42", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
milliSecond = localDateTime.atZone(zoneId).toInstant().toEpochMilli();
System.out.println("milliSecond:" + milliSecond);
//###### ts转字符串时间
instant = Instant.ofEpochMilli(milliSecond);
date = LocalDateTime.ofInstant(instant, zoneId).toString();
System.out.println("date:" + date);
/**********************************************************************************/
//时区写法3:
timezone = "+8";
//###### 字符串时间转ts
zoneId = ZoneId.of(timezone);
localDateTime = LocalDateTime.parse("2018-06-26 16:29:42", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
milliSecond = localDateTime.atZone(zoneId).toInstant().toEpochMilli();
System.out.println("milliSecond:" + milliSecond);
//###### ts转字符串时间
instant = Instant.ofEpochMilli(milliSecond);
date = LocalDateTime.ofInstant(instant, zoneId).toString();
System.out.println("date:" + date);
/**********************************************************************************/

输出结果如下:

milliSecond:1530001782000
date:2018-06-26T16:29:42
milliSecond:1530001782000
date:2018-06-26T16:29:42
milliSecond:1530001782000
date:2018-06-26T16:29:42

JAVA-日期处理-LocalDate-时区

原文:https://www.cnblogs.com/yangh2016/p/14745591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!