Java Instant类

Java Instant类用于表示时间线上的特定时刻。它继承自Object类并实现了Comparable接口。

Java Instant类声明

让我们来看一下java.time.Instant类的声明。

javaCopy code
public final class Instant extends Object  
    implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable 

Java Instant的方法

方法描述
Temporal adjustInto(Temporal temporal)将指定的时间对象调整为此时刻。
int get(TemporalField field)从此时刻中获取指定字段的值。
boolean isSupported(TemporalField field)检查是否支持指定的字段。
Instant minus(TemporalAmount amountToSubtract)返回减去指定数量的时间的此时刻的副本。
static Instant now()从系统时钟获取当前时刻。
static Instant parse(CharSequence text)从文本字符串(如2007-12-03T10:15:30.00Z)获取Instant实例。
Instant plus(TemporalAmount amountToAdd)返回加上指定数量的时间的此时刻的副本。
Instant with(TemporalAdjuster adjuster)返回此时刻的调整副本。
Instant plus(long amountToAdd, TemporalUnit unit)返回加上指定数量的时间的此时刻的副本。
OffsetDateTime atOffset(ZoneOffset offset)将此时刻与偏移量组合以创建OffsetDateTime。
ZonedDateTime atZone(ZoneId zone)将此时刻与时区组合以创建ZonedDateTime。
int compareTo(Instant otherInstant)将此时刻与指定的Instant进行比较。
boolean equals(Object otherInstant)检查此时刻是否与指定的Instant相等。
static Instant from(TemporalAccessor temporal)从Temporal对象获取Instant实例。
int get(TemporalField field)从此时刻中获取指定字段的值。
long getEpochSecond()获取从Java纪元1970-01-01T00:00:00Z开始的秒数。
long getLong(TemporalField field)从此时刻中获取指定字段的值。
int getNano()获取距离秒的开始时刻后沿时间线后的纳秒数。
int hashCode()返回此时刻的哈希码。
boolean isAfter(Instant otherInstant)检查此时刻是否在指定的Instant之后。
boolean isBefore(Instant otherInstant)检查此时刻是否在指定的Instant之前。
static Instant ofEpochMilli(long epochMilli)使用1970-01-01T00:00:00Z的纪元毫秒数获取Instant实例。
static Instant ofEpochSecond(long epochSecond)使用1970-01-01T00:00:00Z的纪元秒数获取Instant实例。
Instant truncatedTo(TemporalUnit unit)返回截断到指定单元的Instant的副本。
long until(Temporal endExclusive, TemporalUnit unit)计算以指定单位表示的时间到另一个Instant之间的时间量。
String toString()返回使用ISO-8601表示的Instant的字符串表示形式。

Java Instant示例:parse()

InstantExample1.java

import java.time.Instant;
public class InstantExample1 {
   public static void main(String[] args) {
      Instant inst = Instant.parse("2017-02-03T10:37:30.00Z");
      System.out.println(inst);
   }
}

输出:

2017-02-03T10:37:30Z

Java Instant示例:now()

InstantExample2.java

import java.time.Instant;
public class InstantExample2 {
   public static void main(String[] args) {
      Instant instant = Instant.now();
      System.out.println(instant);
   }
}

输出:

2017-02-03T06:11:01.194Z

Java Instant示例:minus()

InstantExample3.java

import java.time.*;
public class InstantExample3 {
   public static void main(String[] args) {
      Instant instant = Instant.parse("2017-02-03T11:25:30.00Z");
      instant = instant.minus(Duration.ofDays(125));
      System.out.println(instant);
   }
}

输出:

2016-10-01T11:25:30Z

Java Instant示例:plus()

InstantExample4.java

import java.time.*;
public class InstantExample4 {
   public static void main(String[] args) {
      Instant inst1 = Instant.parse("2017-02-03T11:25:30.00Z");
      Instant inst2 = inst1.plus(Duration.ofDays(125));
      System.out.println(inst2);
   }
}

输出:

2017-06-08T11:25:30Z

Java Instant示例:isSupported()

InstantExample5.java

import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class InstantExample5 {
   public static void main(String[] args) {
      Instant inst = Instant.parse("2017-02-03T11:35:30.00Z");
      System.out.println(inst.isSupported(ChronoUnit.DAYS));
      System.out.println(inst.isSupported(ChronoUnit.YEARS));
   }
}

输出:

true
false

标签: java, Java面试题, Java下载, java教程, java技术, Java学习, Java学习教程, Java语言, Java开发, Java入门教程, Java进阶教程, Java高级教程, Java笔试题, Java编程思想