首页 > Java > java教程 > 正文

Java本地时间

WBOY
发布: 2024-08-30 15:49:48
原创
580 人浏览过

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()方法。创建了一个类,然后主类后面跟着方法调用和简单的输出语句。执行后,它将返回当前系统时间。输出的格式为“小时”、“分钟”、“秒”和“小秒”。请参阅下面的屏幕截图以获取输出。

输出:

Java本地时间

示例#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:

Java本地时间

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:

Java本地时间

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!