日期时间  <-->  时间戳

java.time 包提供的新的日期和时间API

LocalDateTime: 本地日期时间类

ZoneId: 时区类

ZonedDateTime: 带时区的日期时间类

Instant: 高精度时间戳

long: 长整型

LocalDateTime + ZoneId = ZonedDateTime

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime; public class Main {
public static void main(String[] args) {
// 直接获取 ZonedDateTime 对象
ZonedDateTime zdt1 = ZonedDateTime.now();
System.out.println(zdt1); // LocalDateTime + ZoneId = ZonedDateTime
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt2 = ldt.atZone(ZoneId.systemDefault());
System.out.println(zdt2);
}
} 运行结果:
2019-10-29T14:01:16.169741500+08:00[Asia/Shanghai]
2019-10-29T14:01:16.170744+08:00[Asia/Shanghai]

ZonedDateTime --> LocalDateTime

import java.time.LocalDateTime;
import java.time.ZonedDateTime; public class Main {
public static void main(String[] args) {
// 直接获取 ZonedDateTime 对象
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(zdt); //转换为LocalDateTime时,直接丢弃了时区信息
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
} 运行结果:
2019-10-29T14:00:13.876198+08:00[Asia/Shanghai]
2019-10-29T14:00:13.876198

ZonedDateTime -->  Instant  -->  long

import java.time.Instant;
import java.time.ZonedDateTime; public class Main {
public static void main(String[] args) {
// 直接获取 ZonedDateTime 对象
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(zdt);
// to Instants
Instant ins = zdt.toInstant();
System.out.println(ins);
// to long
System.out.println(ins.toEpochMilli());
}
} 运行结果:
2019-10-29T14:11:00.265786900+08:00[Asia/Shanghai]
2019-10-29T06:11:00.265786900Z
1572329460265

long  -->  Instant  -->  ZonedDateTime

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime; public class Main {
public static void main(String[] args) {
// 获取 unix 毫秒值
long insMilli = Instant.now().toEpochMilli();
System.out.println(insMilli); // to Instant
Instant ins = Instant.ofEpochMilli(insMilli);
// to ZonedDateTime
ZonedDateTime zdt = ZonedDateTime.ofInstant(ins, ZoneId.systemDefault());
System.out.println(zdt);
}
} 运行结果:
1572330350986
2019-10-29T14:25:50.986+08:00[Asia/Shanghai]

ending ~

Java 日期时间与unix时间戳之间转换的更多相关文章

  1. Linux命令date日期时间和Unix时间戳互转

    A.将日期转换为Unix时间戳将当前时间以Unix时间戳表示: date +%s 输出如下: 1361542433 转换指定日期为Unix时间戳: date -d '2013-2-22 22:14' ...

  2. C#中系统时间和UNIX时间戳互相转换

    在项目开发过程中,有时会遇到不同程序之间相互调用数据,数据中不免会包含时间,比如ASP.NET调用PHP,牵扯到时间就要做一下处理,PHP程序中一般存取的都是UNIX时间,不像ASP.NET存储的是年 ...

  3. Java日期时间API系列19-----Jdk8中java.time包中的新的日期时间API类,ZonedDateTime与ZoneId和LocalDateTime的关系,ZonedDateTime格式化和时区转换等。

    通过Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类中时间范围示意图:可以很清晰的看出ZonedDateTime相当于LocalDateTime+ZoneI ...

  4. paip.日期时间操作以及时间戳uapi php java python 总结

    paip.日期时间操作以及时间戳uapi php java python 总结 ///uapi Date 函数 | Day 函数 | Hour 函数 | Minute 函数 | Month 函数 | ...

  5. Java 日期时间 Date类型,long类型,String类型表现形式的转换

    Java 日期时间 Date类型,long类型,String类型表现形式的转换 1.java.util.Date类型转换成long类型 java.util.Date dt = new Date(); ...

  6. Java 日期时间 Date类型,long类型,String类型表现形式的转换 (转)

    Java 日期时间 Date类型,long类型,String类型表现形式的转换 1.java.util.Date类型转换成long类型java.util.Date dt = new Date();Sy ...

  7. python datetime和unix时间戳之间相互转换

                                python datetime和unix时间戳之间相互转换 1.代码:    import time    import datetime # ...

  8. 一文告诉你Java日期时间API到底有多烂

    前言 你好,我是A哥(YourBatman). 好看的代码,千篇一律!难看的代码,卧槽卧槽~其实没有什么代码是"史上最烂"的,要有也只有"史上更烂". 日期是商 ...

  9. Java 日期时间

    Java 日期时间 标签 : Java基础 Date java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含 ...

随机推荐

  1. 用Visual Studio编写UDF的一点小技巧(二)

  2. Invalid bound statement (not found) 错误原因

    对我来说,错误的原因是因为没有配置:mybatis.mapperLocations=classpath:mybatis/mapper/*Mapper.xmlmybatis.config-locatio ...

  3. Deep High-Resolution Representation Learning for Human Pose Estimation

    Deep High-Resolution Representation Learning for Human Pose Estimation 2019-08-30 22:05:59 Paper: CV ...

  4. Websocket实现Java后台主动推送消息到前台

    写在前面 需求: 项目测试, 缺少用户登录失败给admin推送消息, 想到这个方式, 当用户登录失败时, admin用户会在页面看到咣咣乱弹的alert. 正文 pom.xml <!-- web ...

  5. SQLServer 截取函数 substring函数

    declare @name char(1000) --注意:char(10)为10位,要是位数小了会让数据出错 set @name='s{sss}fc{fggh}dghdf{cccs}x' selec ...

  6. c# winform访问 带有windows身份验证的webservice

    1 将webservice设置为windows身份验证iis10中,要确认已安装windows身份验证在 控制面板 - >打开或关闭Windows功能 - >万维网服务 - >安全性 ...

  7. 【Js】单页面多个倒计时问题

    代码: <!DOCTYPE html> <html> <head> <title>多个timeout</title> <script ...

  8. git pull的时候提示git pull <remote> <branch>

    yuanqiao@yuanqiao-PC MINGW64 /h/WorkSpace/git/dadeTest (dev)$ git pullremote: Enumerating objects: 7 ...

  9. 【python基础】Python性能加速的方法

    参考 1. 给Python加速(性能加速的方法); 2. pythonSpeed_PerformanceTips; 完

  10. [LeetCode] 261. Graph Valid Tree 图是否是树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...