目录
语法
Java Instant 的方法
Examples to Implement Java Instant
1. toString()
2. getEpochSecond()
3. getNano()
4. plusSeconds()
5. compareTo()
Conclusion
首页 Java java教程 Java即时

Java即时

Aug 30, 2024 pm 03:49 PM
java

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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

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

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP与Python:了解差异 PHP与Python:了解差异 Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP与其他语言:比较 PHP与其他语言:比较 Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP与Python:核心功能 PHP与Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

Java程序查找胶囊的体积 Java程序查找胶囊的体积 Feb 07, 2025 am 11:37 AM

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP:许多网站的基础 PHP:许多网站的基础 Apr 13, 2025 am 12:07 AM

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP的影响:网络开发及以后 PHP的影响:网络开发及以后 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles