Java LocalTime 是内置的功能包之一,可用于与日期和时间相关的多种操作。对于与 LocalTime 相关的操作,Java 有一个名为 Java Time 的特定应用程序接口 (API),它拥有多种方法可将其应用于任何与日期和时间相关的函数。 LocalTime 类中常用的一些方法有 get()、compareTo()、equals、atDate()、of()、now()、plusHours()、minusHours() 等,以小时、分钟和秒为单位预定义对象。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
现在我们已经了解了 Java 8 的 LocalTime 类,让我们学习语法。
语法:
public final class LocalTime extends Object Implements Comparable
以上语法是初始化类的标准方法。如前所述,LocalTime 类扩展了 Object 类并实现了类似的接口。
Java LocalTime 的方法
现在让我们探索 LocalTime 类提供的方法。
public LocalDateTime atDate(LocalDate date)
1。 atDate():要创建 LocalDateTime,它将该时间与日期结合起来。将要组合的日期作为参数,不接受 null。在这里,所有可能的日期和时间组合都是有效的。
public int compareTo(LocalTime other)
2。 compareTo(): 简单地将此时间与任何其他经过的时间进行比较。将要比较的另一个时间作为参数。返回比较值。如果较大则为正,如果较小则为负。如果另一个值未传递或未能传递,它会抛出 NullPointerException。
public int get(TemporalField field)
3。 get(): 获取此时的整数值。将要获取的字段作为参数,不接受 Null。并返回该字段的值。返回值始终在范围内。如果由于任何原因无法返回值,则会抛出异常。
4。异常: 如果值超出范围或无法获取值,则抛出 DateTimeException。如果发生数字溢出,则会抛出 ArithmeticException。
public boolean equals(Object obj)
5。等于: 此时间与任何其他时间之间的简单比较。它接受一个对象作为参数进行检查,如果值为 false,则返回 null。它覆盖类对象中的 equals。这里只比较LocalTime类型的对象;如果是其他类型,则返回 false。
public static LocalTime now()
6。 now(): 在默认时区,now()从系统获取当前时间。 Never Null 始终返回当前系统时间。
public static LocalTime now(ZoneId zone)
7。 now(ZoneId zone): 与上面类似,但具有指定的时区。它以区域 ID 作为参数,不接受 null。返回指定时区的当前时间。
public static LocalTime of(int hour, int minute, int second, int nanoOfSecond):
8。 of(): 获取 LocalTime 的实例。采用小时、分钟、秒、纳秒四个参数,并返回本地时间,绝不为空。如果值碰巧超出范围,则会抛出 DateTimeException。
public LocalTime minusHours(long hoursToSubtract):
9。 minusHours(): 返回此 LocalTime 的副本并减去小时数。以hoursToSubtract为参数;它不能是负数。并返回此 LocalTime,减去小时数。这里的实例是不可变的,不受方法调用的影响。
public LocalTime plusHours(long hoursToAdd):
10。 plusHours(): 与上面提到的方法完全相反。返回此 LocalTime 的副本,并添加了指定的小时数。与上面类似,它是不可变的且不受影响。
实现 Java LocalTime 的示例
现在我们已经了解了上面的方法,让我们尝试用示例来演示这些方法。
示例#1
代码:
public class localtime { public static void main(String[] args) { LocalTime time_now = LocalTime.now(); System.out.println(time_now); } }
代码解读:例如例1,我们简单地实现了LocalTime类的now()方法。创建了一个类,然后主类后面跟着方法调用和简单的输出语句。执行后,它将返回当前系统时间。输出的格式为“小时”、“分钟”、“秒”和“小秒”。请参阅下面的屏幕截图以获取输出。
输出:
示例#2
代码:
import java.time.LocalTime; public class localtime { public static void main(String[] args) { LocalTime time_1 = LocalTime.of(10,43,12); System.out.println(time_1); LocalTime time_2=time_1.minusHours(3); LocalTime time_3=time_2.minusMinutes(41); System.out.println(time_3); } }
Code Interpretation: Here, we have demonstrated two methods, minusHours() and minusMinutes(). After creating our class and main class, we called the () method with parameters and printed the next line’s output. Later, we created two objects with two methods, respectively, for hours and for minutes. So, out of the given time in of() method, the number of hours to be subtracted will be 2, and minutes will be 41. With that in mind, our output should be 10-3 hours, which is 7 hours and 43-41 minutes, which will be 2 minutes. So, the final output must be “07:02:12”. For sample output, refer to the below attached screenshot.
Output:
Example #3
Code:
import java.time.*; public class localtime { public static void main(String[] args) { LocalTime time1 = LocalTime.parse("13:08:00"); LocalTime time_now = LocalTime.now(); System.out.println("LocalTime1: " + time1); System.out.println("LocalTime2: " + time_now); boolean eq_value = time1.equals(time_now); System.out.println("Both times are equal: " + eq_value); } }
Code Interpretation: In our 3rd example, we have implemented the equals method. Other than the creation of class and the main class, we have two objects with assigned values. At first, we have passed a specific time, and for the second, we have fetched the current time of the system with the now() method. Later, we have printed these two values and then a boolean comparison. Using the equals() method, we have compared the two times and passed the output to the output print statement. The equals method’s output will be either true or false; based on the values passed here, the output here must be false. Refer below the screenshot for output.
Output:
Conclusion
Java 8’s Date Time update comes with many features, and LocalTime is one of them, which does not store any value but is a better representation of the date and time. We understood the methods with description and followed by three example samples. LocalTime class of java provides a wide range of methods, more than mentioned and can be used as per requirement.
以上是Java本地时间的详细内容。更多信息请关注PHP中文网其他相关文章!

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

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

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

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

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

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

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

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


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器