搜索

Java Instant 是 Java 中一组预定义的方法,可以在代码块中使用它们来执行程序中的特定功能。一些最常用的 Java Instant 方法是 toString()、parse()、ofEpochMilli()、getEpochSecond()、getNano()、plusMillis(long milliToAdd)、plusNanos(long nanosToAdd)、plusSeconds(long SecondsToAdd)、minusSeconds (long SecondsToSubtract)、minusMillis(long millisToSubtract)、minusNanos(long nanosToSubtract)、compareTo(Instant otherInstant)、isAfter(Instant otherInstant) 和 isBefore(Instant otherInstant)。 Java 中的这一特性以其更高的效率、卓越的性能、可重用性、优化的代码长度等而闻名

语法

Instant类提供了多个静态工厂方法来创建Instant类的实例。以下是我们可以获取不同标准值的 Instant 类实例的静态方法。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Instant instant = Instant.now();

now() 方法返回系统时钟的当前时刻。该实例表示从开始时间或 EPOCH 开始的纳秒总数。这是从 1970 年 1 月 1 日开始计算的时间。这种获取瞬间的方法对于表示机器时间戳很有用,并且会比其他方法更频繁地使用。

Instant instant = Instant.EPOCH;

此静态字段将返回表示确切 EPOCH 时间的 Instant 类的实例。

Instant instant = Instant.MAX;

这个静态字段将返回一个 Instant 类的实例,表示该实例的最大值。

Instant instant = Instant.MIN;

这个静态字段会返回一个Instant类的Instance,代表instance的最小值,值为负数。

Java Instant 的方法

接下来就是Instant类的主要部分或者说用法了。以下是 Instant 类的一些主要方法的列表。

  • toString(): 此方法从 Object 类重写,以使用 ISO-8601 标准以字符串格式表示实例。

以下是可用于获取 Instant 类实例的方法。

  • parse(): 此方法采用文本字符串作为参数,并返回表示传递的相同值的 Instance 类的实例。该字符串应在 UTC 时间立即有效。
  • ofEpochMilli(): 此方法将输入长(毫秒)作为参数,并返回表示传递的相同值的 Instance 类的实例。该方法可用于将 java.util.Date 对象转换为 Instant.

Instant 类的实例表示时间的精度为纳秒,而 util.Date 的精度为毫秒。因此,瞬间需要更多的存储空间来存储其值(大于长)。因此,即时类将秒的值存储在 long 变量中,并将剩余的纳秒值存储在 int 变量中。我们可以使用以下方法访问这些单独的值。

  • getEpochSecond(): 此方法将仅返回 EPOCH 中的秒数。
  • getNano(): 此方法返回从时间线或秒开始算起的纳秒数。

以下是可用于即时操作或计算的方法。

  • plusMillis(long millisToAdd):此方法将添加瞬时经过的许多毫秒并返回新的瞬时。
注意:请注意,Instant 类是一个不可变类,因此它将为每个操作操作返回一个新实例。
  • plusNanos(long nanosToAdd):与上一个类似,但增加了纳秒。
  • plusSeconds(long SecondsToAdd):秒的添加。

减法或减法运算也有类似的方法。这些方法将从该时刻减去经过的秒数并返回一个新的时刻。

  • 减秒(长秒减去)
  • minusMillis(long millisToSubtract)
  • minusNanos(长 nanosToSubtract)

以下是可用于比较两个时刻的方法,

  • compareTo(Instant otherInstant): This method will compare the one instant with the passed one. Returns the integer value negative if it is less and positive if it is greater.
  • isAfter(Instant otherInstant): This method checks if the passed instant is after the current instant. Returns true or false.
  • isBefore(Instant otherInstant): Similar to the previous one, checks if the passed instant is before the current instant. Returns true or false.

Examples to Implement Java Instant

Below are the examples of implementing java instant:

1. toString()

Code:

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

Output:

Java即时

The output will be the time of the system running the code.

2. getEpochSecond()

Code:

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

Output:

Java即时

3. getNano()

Code:

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

Output:

Java即时

4. plusSeconds()

Code:

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

Output:

Java即时

We have added exactly 3600 seconds, i.e. 1 hour, to the current instant, as it can be seen in the output that time is increased by 1 hour.

5. compareTo()

Code:

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

Output:

Java即时

Here, we are comparing two instants, current with the instant having added 1 more hour. As the current instance is less than instance2, the output is negative.

Conclusion

So, we have seen the Instant class introduced from Java 8. This class represents the seconds from the start of the timeline with nanoseconds precision. This class is useful to generate the timestamp, which will represent the system time. We have seen different types of instants which we can get and different methods useful for calculations, creating a new instantly, comparing instants, getting seconds and nanoseconds differently, etc. Instant class is immutable and provides thread safety as compared to the old Date object. The Instant class is much more powerful than the old time-date API.

以上是Java即时的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

一文掌握Java8新特性Stream流的概念和使用一文掌握Java8新特性Stream流的概念和使用Jun 23, 2022 pm 12:03 PM

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器