Java Instant类
时间点
此类的对象表示时间线上的一点。可以理解为人类的绝对时间
。
因为是时间线的一点,所以时间点可以比较大小,那么这个时间点的标准都是统一的(UTC时间);即这个时间点对于整个地球人们来说是唯一的,不是北京时间、也不是东京时间、而是世界时间。
与时间点相关的两个概念就是时间线和时间的单位
。
一、时间线
时间点在时间线上(点在线上)。时间线上有三个重要的点:最大值点、最小值点、原点(时间不知道有没有尽头)。
在Java世界,时间线的末端分别是Instant.MAX和Instant.MIN;时间线的原点是Instant.EPOCH
(1)时间线上的原点
System.out.println(Instant.EPOCH);
结果:1970-01-01T00:00:00Z
(2)时间线的最大值
System.out.println(Instant.MAX);
结果:+1000000000-12-31T23:59:59.999999999Z
(3)时间线的最小值
System.out.println(Instant.MIN);
结果:-1000000000-01-01T00:00:00Z
二、时间的单位
年、月、日、时、分、秒都是时间的单位。
现在人们把控自己生活节奏已经到了分钟这个单位;而科技的进步要求把控我们的机器的时间单位是秒、毫秒、纳秒。
在时间线上,我们使用秒(s)作为时间点的单位
。
1s(1秒)= 1000ms(1000毫秒) = 1000_000us(微妙)=1000_000_000ns(纳秒)
1day(天)=86400s(秒)=8640000ms(秒)
1year(年)= 365.2425day(天) = 31556952s(秒)= 31556952000ms(毫秒)
在Instant对象中,有一个long长度的值表示距离原点的长度(单位秒),一个int长度的值表示纳秒(永远是正数,不会大于999_999_999;因为1000_000_000ns = 1s
)。
源码:
/*** The number of seconds from the epoch of 1970-01-01T00:00:00Z.*/
private final long seconds;
/*** The number of nanoseconds, later along the time-line, from the seconds field.* This is always positive, and never exceeds 999,999,999.*/
private final int nanos;
三、时间点的构造
- (1)当前时间点
Instant now = Instant.now();System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());
结果:
2019-04-18T08:04:29.633Z
1555574669
633000000
1555574669633
- (2)根据距离原点的秒构造时间点
这个构造方法不够精准,无法到纳秒级。
①距离原点1秒
Instant now = Instant.ofEpochSecond(1L);System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());
结果:
1970-01-01T00:00:01Z
1
0
1000
②距离原点-1秒
Instant now = Instant.ofEpochSecond(-1L);System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());
结果:
1969-12-31T23:59:59Z
-1
0
-1000
- (3)构造到纳秒级的时间点
源码:
public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);return create(secs, nos);}
这个方法有两个参数,第一个是秒,第二个是可以为正负的纳秒值。
Instant now = Instant.ofEpochSecond(5L,500L);
System.out.println(now);
System.out.println(now.getEpochSecond());
System.out.println(now.getNano());
System.out.println(now.toEpochMilli());
1970-01-01T00:00:05.000000500Z
5
500
5000
Instant now = Instant.ofEpochSecond(5L,-500L);System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());
1970-01-01T00:00:04.999999500Z
4
999999500
4999
- (4)构造到毫秒级的时间点
Instant now = Instant.ofEpochMilli(5500L);System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());
1970-01-01T00:00:05.500Z
5
500000000
5500
- (5)根据文本构造时间点
对字符串的格式有要求,字符串必须如同: 2007-12-03T10:15:30.00Z
Instant now = Instant.parse("1007-12-03T10:15:30.00Z");System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());
1007-12-03T10:15:30Z
-30360318270
0
-30360318270000
四、时间点的比较
A.compareTo(B):A>B,return 1;A= B,return 0;A<B,return -1;
Instant datetime1 = Instant.parse("2007-12-03T10:15:30.00Z");
Instant datetime2 = Instant.parse("2017-12-03T10:15:30.00Z");
Instant datetime3 = Instant.parse("2019-12-03T10:15:30.00Z");System.out.println(datetime2.compareTo(datetime3));
System.out.println(datetime2.compareTo(datetime2));
System.out.println(datetime2.compareTo(datetime1));
System.out.println(datetime2.isAfter(datetime2));
System.out.println(datetime2.isBefore(datetime3));
-1
0
1
false
true
五、时间点的运算
- (1)时间点加秒级、毫秒、纳秒
①原点加100000s
Instant plusSeconds = Instant.EPOCH.plusSeconds(100000L);System.out.println(plusSeconds);System.out.println(plusSeconds.getEpochSecond());System.out.println(plusSeconds.toEpochMilli());System.out.println(plusSeconds.getNano());
1970-01-02T03:46:40Z
100000
100000000
0
- (2)时间点加任意时间单位
①原点加一百天
Instant plus100days = Instant.EPOCH.plus(100, ChronoUnit.DAYS);
System.out.println(plus100days);
System.out.println(plus100days.getEpochSecond());
System.out.println(plus100days.toEpochMilli());
System.out.println(plus100days.getNano());
1970-04-11T00:00:00Z
8640000
8640000000
0
文章转自
Java Instant类相关推荐
- Java常用类(2)--日期时间相关类Date、Calendar、LocalDateTime、Instant全面
文章目录 java.lang.System类 java.util.Date类 java.sql.Date类 java.text.SimpleDateFormat类 java.util.Calendar ...
- JAVA时间戳类Instant
JAVA时间戳类Instant 前言 在JAVA8之前的版本,去获取时间戳(毫秒级别)常用的办法有两种 // 方法一:构建日期Date类然后调用getTime方法 Date date = new Da ...
- java range类_Java即时类| range()方法与示例
java range类 即时类range()方法 (Instant Class range() method) range() method is available in java.time pac ...
- 返回值 包装类_(九)Java常用类
(九)Java常用类 String类 概述 /*String:字符串,使用一对""来表示. * 1.String声明为final,不能被继承. * 2.String实现了Seria ...
- Java基础知识——Java常用类的使用
目录 内容小结 String类 String对象的创建 字符串的特性判断 拼接 String类的常用方法 String和char[]的相互转换 实现代码 StringBuffer StringBuff ...
- Iso时间转java instant,Java /将ISO-8601(2010-12-16T13:33:50.513852Z)转换为
Instant.parse( "2010-12-16T13:33:50.513852Z" ) java.time 较新的java.time类可以处理此字符串输入. 该Z对端是短期的 ...
- JDK8 Instant类的使用和LocalDate,LocalTime 和 LocalDateTime的介绍。
简介 在Java 8中又为我们提供了新的日期类.它可以帮我们更精确的保存记录时间. 本文章主要介绍的就是Instant,LocalDate,LocalTime 和 LocalDateTime 这几个类 ...
- Instant类使用
import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset;public class ...
- Java Util 类
Java Util类 字符串 类型转化 //将其他类型转化为字符串类型 int a = 1234567; String val = String.valueOf(a); System.out.prin ...
最新文章
- Test execution order
- .net 2.0 BackgroundWorker类详细用法
- MEET2020 | 嘉宾已确认!李开复、倪光南等AI大咖齐聚,共话人工智能新价值新边界新格局...
- 中修改环境变量_超详干货!Linux环境变量配置全攻略
- linux 修改文件时间
- mysql 征途_MySQL数据库
- Mongodb定时备份脚本和清除脚本
- Java 统计字母个数
- 用 Python 下载抖音无水印视频
- pclose与fclose的区别
- 谷歌 Fuchsia OS 进入开发者测试阶段,它真的会代替安卓吗?
- System.IO.Path 操作
- 大众点评后端项目解析
- 修改窗口的风格ModifyStyle
- 程序猿12个人艰不拆的真相
- c /c++语法之extern关键字
- 4.Mybatis 环境搭建
- 基于SVM的中文垃圾短信分类
- 极光笔记 | 极光推送业务无中断迁移上云实践
- round函数c语言,fegetround
热门文章
- 神经网络neural network简单理解
- 配置zabbix监控windows,cmd运行报错cannot connect to Service Manager: [0x00000005]
- 感悟:微博深度学习平台架构和实践
- 数论-扩展中国剩余定理
- 网站自动登录功能的设计
- 如何建立软件测试管理体系?
- SharePoint 2013 Error - TypeError: Unable to get property 'replace' of undefined or null reference
- Spring MVC 入门
- [英]Promises Don't Come Easy
- 小波去噪MATLAB代码