Instant
Instant 表示一个时间轴上的瞬时时刻,它是计算机读的时间,它是以Unix元年时间(传统的设定为UTC时区1970年1月1日00:00:00)开始经历的秒数进行计算;
Instant由两部分组成,UTC时区1970年1月1日00:00:00到现在的秒数,和剩余的纳秒数,如:2018-11-19T02:30:33.753640900Z,2018-11-19T02:30:33和753640900
Instant常用方法
Instant是一个常用类,并且内置了许多静态工厂方面,所以获取一个Instant实例非常的方便。
1. Instant的静态工厂方法
- Instant.now():获取当前时间
System.out.println(Instant.now()); -----------------输出---------------------- 2018-11-19T02:40:25.704913500Z
- Instant.now(Clock clock):获取当前时间
System.out.println(Instant.now(Clock.systemUTC())); -----------------输出---------------------- 2018-11-19T02:43:20.444604800Z
- Instant.ofEpochMilli(long epochMilli):获取Instant,使用从1970-01-01T00:00:00Z的时代起的毫秒
System.out.println(Instant.ofEpochMilli(100)); -----------------输出---------------------- 1970-01-01T00:00:00.100Z
- Instant.ofEpochSecond(long epochSecond):获取Instant,从1970-01-01T00:00:00Z的时代起的秒。
System.out.println(Instant.ofEpochSecond(100)); -----------------输出---------------------- 1970-01-01T00:01:40Z
- Instant.ofEpochSecond(long epochSecond, long nanoAdjustment):获取Instant从1970-01-01T00:00:00Z时代起的秒和纳秒分数开始使用秒数。(指定秒和纳秒)
System.out.println(Instant.ofEpochSecond(100,100)); -----------------输出---------------------- 1970-01-01T00:01:40.000000100Z
- Instant.parse(CharSequence text):把文本转换为Instant对象(格式必须为Instant格式)
System.out.println(Instant.parse("1970-01-01T00:01:40.000000100Z")); -----------------输出---------------------- 1970-01-01T00:01:40.000000100Z
- Instant.from(TemporalAccessor temporal):从一个时间存取器中获取一个Instant对象
System.out.println(Instant.from(OffsetDateTime.now())); -----------------输出---------------------- 2018-11-19T03:02:52.348777Z
OffsetDateTime和Instant都是实现的Temporal接口,Temporal是TemporalAccessor的子接口,所以也这个两个常用类也实现了TemporalAccessor接口
2.实现 TemporalAccessor接口的方法
- public int get(TemporalField field):获取指定字段的值,注意这里的字段只支持一秒之内的字段:纳秒,微秒,毫秒。
Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.get(ChronoField.MILLI_OF_SECOND)); System.out.println(instant.get(ChronoField.MICRO_OF_SECOND)); System.out.println(instant.get(ChronoField.NANO_OF_SECOND)); -----------------输出---------------------- 2018-11-19T03:11:50.751710100Z 751 751710 751710100
- public long getLong(TemporalField field):也是获取指定的值,但是是long类型,而且支持获取从 Unix元年开始的秒数
Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.get(ChronoField.MILLI_OF_SECOND)); System.out.println(instant.get(ChronoField.MICRO_OF_SECOND)); System.out.println(instant.get(ChronoField.NANO_OF_SECOND)); System.out.println(instant.getLong(ChronoField.INSTANT_SECONDS)); -----------------输出---------------------- 2018-11-19T03:11:50.751710100Z 751 751710 751710100 1542597577
- boolean isSupported(TemporalField field):检查是否支持指定字段
Instant instant = Instant.now(); System.out.println(instant.isSupported(ChronoField.NANO_OF_SECOND)); -----------------输出---------------------- true
- public R query(TemporalQuery query):查询日期,如查询某一天
Instant instant = Instant.now(); ValueRange query = instant.query(x -> { System.out.println(x.isSupported(ChronoField.NANO_OF_SECOND)); return x.range(ChronoField.NANO_OF_SECOND); }); System.out.println(query); -----------------输出---------------------- true 0 - 999999999
上面的代码可以在Lambda表达式中进行进一步从查询,可以返回任意类型
- public ValueRange range(TemporalField field):查询指定字段的有效值范围
Instant instant = Instant.now(); System.out.println(instant.range(ChronoField.MILLI_OF_SECOND)); -----------------输出---------------------- 0 - 999
3.实现 Temporal接口的方法
- public boolean isSupported(TemporalUnit unit):检查是否支持指定的时间单位
Instant instant = Instant.now(); System.out.println(instant.isSupported(ChronoUnit.SECONDS)); -----------------输出---------------------- true
- public Instant minus(TemporalAmount amountToSubtract):减去指定的时间量
Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.minus(Duration.of(1,ChronoUnit.SECONDS))); -----------------输出---------------------- 2018-11-19T07:31:54.791401900Z 2018-11-19T07:31:53.791401900Z
Duration和period表示持续的时间都实现了TemporalAmount接口
- public Instant minus(long amountToSubtract, TemporalUnit unit):根据单位减去指定的时间量
Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.minus(1,ChronoUnit.SECONDS)); -----------------输出---------------------- 2018-11-19T07:34:49.595281800Z 2018-11-19T07:34:48.595281800Z
- public Instant plus(TemporalAmount amountToAdd):加上指定的时间量(同理上面的例子2)
- public Instant plus(long amountToAdd, TemporalUnit unit):同理上面的例子3,只是是加法
- public long until(Temporal endExclusive, TemporalUnit unit) :根据指定的单位计算现在到另一个时间点的时间量
Instant instant = Instant.now(); System.out.println(instant.until(Instant.now(),ChronoUnit.NANOS)); -----------------输出---------------------- 10912400
- public Instant with(TemporalAdjuster adjuster):时间校正
- public Instant with(TemporalField field, long newValue):根据指定的时间单位进行时间校正
Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.with(Instant.MAX)); System.out.println(instant.with(ChronoField.NANO_OF_SECOND,1)); -----------------输出---------------------- 2018-11-19T07:47:24.526743200Z +1000000000-12-31T23:59:59.999999999Z 2018-11-19T07:47:24.000000001Z
4.实现TemporalAdjuster的方法
Temporal adjustInto(Temporal temporal);将指定的时间对象校正为现在的瞬时对象
Instant instant = Instant.now();
System.out.println(instant);
System.out.println(instant.adjustInto(Instant.MAX));
-----------------输出----------------------
2018-11-19T08:04:35.904851500Z
2018-11-19T08:04:35.904851500Z
5.实现Comparable的方法
public int compareTo(Instant otherInstant):将现在的瞬时对象与指定的对象进行对比
Instant instant = Instant.now();
System.out.println(instant.compareTo(Instant.MAX));
-----------------输出----------------------
-1
如果结果为负,就是小于,正就是大于,相等于为0
6.普通方法
- 加减方法:
plusMillis(long millisToAdd):增加毫秒数
plusNanos(long nanosToAdd):增加纳秒
plusSeconds(long secondsToAdd):怎加秒
minusMillis(long millisToSubtract):减少毫秒
minusNanos(long nanosToSubtract):减少纳秒
minusSeconds(long secondsToSubtract):减少秒Instant instant = Instant.now(); System.out.println(instant); System.out.println("加100纳秒:"+instant.plusNanos(100)); System.out.println("加100秒:"+instant.plusSeconds(100)); System.out.println("加100豪秒:"+instant.plusMillis(100)); System.out.println("减100纳秒:"+instant.minusNanos(100)); System.out.println("减100秒:"+instant.minusSeconds(100)); System.out.println("减100毫秒:"+instant.minusMillis(100)); -----------------输出---------------------- 2018-11-19T07:57:27.364589400Z 加100纳秒:2018-11-19T07:57:27.364589500Z 加100秒:2018-11-19T07:59:07.364589400Z 加100豪秒:2018-11-19T07:57:27.464589400Z 减100纳秒:2018-11-19T07:57:27.364589300Z 减100秒:2018-11-19T07:55:47.364589400Z 减100毫秒:2018-11-19T07:57:27.264589400Z
- 判断时间前后
isAfter(Instant otherInstant)
isBefore(Instant otherInstant)Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.isBefore(Instant.now()));//true System.out.println(instant.isAfter(Instant.now()));//false
- 按单位获取
toEpochMilli()://当前时间的毫秒
getEpochSecond()://当前时间的秒数
getNano()://当前时间的纳秒 - truncatedTo(TemporalUnit unit):Instant截断到指定的单元。
- atZone(ZoneId zone):将此瞬时与时区结合
- atOffset(ZoneOffset offset):将瞬时与偏移量合在一直
Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.truncatedTo(ChronoUnit.SECONDS)); System.out.println(instant.toEpochMilli()); System.out.println(instant.getNano()); System.out.println(instant.getEpochSecond()); System.out.println(instant.atZone(ZoneId.systemDefault())); System.out.println(instant.atOffset(ZoneOffset.MAX)); -----------------输出---------------------- 2018-11-19T08:16:14.875247500Z 2018-11-19T08:16:14Z 1542615374875 875247500 1542615374 2018-11-19T16:16:14.875247500+08:00[Asia/Shanghai] 2018-11-20T02:16:14.875247500+18:00
其他时间
日期和时间:LocalDate和LocalTime,日期时间LocalDateTime,偏移日期时间offsetDateTime,时区日期时间zonedDateTime
这些都是实现Temporal接口,而且与Instant的方法都类似。
持续时间
Duration:一种基于时间的时间长度,一般用于Instant,
Period:基于年数,月和日的时间长度。