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在企业级应用中被广泛使用是因为其平台独立性。1)平台独立性通过Java虚拟机(JVM)实现,使代码可在任何支持Java的平台上运行。2)它简化了跨平台部署和开发流程,提供了更大的灵活性和扩展性。3)然而,需注意性能差异和第三方库兼容性,并采用最佳实践如使用纯Java代码和跨平台测试。

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)橱柜橱柜:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)

Java适合开发跨服务器web应用。1)Java的“一次编写,到处运行”哲学使其代码可在任何支持JVM的平台上运行。2)Java拥有丰富的生态系统,包括Spring和Hibernate等工具,简化开发过程。3)Java在性能和安全性方面表现出色,提供高效的内存管理和强大的安全保障。

JVM通过字节码解释、平台无关的API和动态类加载实现Java的WORA特性:1.字节码被解释为机器码,确保跨平台运行;2.标准API抽象操作系统差异;3.类在运行时动态加载,保证一致性。

Java的最新版本通过JVM优化、标准库改进和第三方库支持有效解决平台特定问题。1)JVM优化,如Java11的ZGC提升了垃圾回收性能。2)标准库改进,如Java9的模块系统减少平台相关问题。3)第三方库提供平台优化版本,如OpenCV。

JVM的字节码验证过程包括四个关键步骤:1)检查类文件格式是否符合规范,2)验证字节码指令的有效性和正确性,3)进行数据流分析确保类型安全,4)平衡验证的彻底性与性能。通过这些步骤,JVM确保只有安全、正确的字节码被执行,从而保护程序的完整性和安全性。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

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

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

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