本文共 4691 字,大约阅读时间需要 15 分钟。
Java LocalTime class belongs to the .
Java LocalTime类属于 。
LocalTime objects can be created using the below mentioned ways.
可以使用以下提到的方式创建LocalTime对象。
now()
method of LocalTime class. There are two other overloaded now() methods that takes argument as Clock
and ZoneId
.LocalTime lt = LocalTime.now();System.out.println(lt); //15:43:43.212787LocalTime lt1 = LocalTime.now(Clock.systemDefaultZone());System.out.println(lt1); //15:43:43.213454LocalTime lt2 = LocalTime.now(ZoneId.systemDefault());System.out.println(lt2); //15:43:43.213542可以使用LocalTime类的
now()
方法创建LocalTime实例。 还有另外两个重载的now()方法,它们的参数为Clock
和ZoneId
。 of()
method. There are multiple overloaded methods for different arguments for hour, minute, second and nanosecond.LocalTime lt = LocalTime.of(10, 30);System.out.println(lt); //10:30LocalTime lt1 = LocalTime.of(10, 30, 45);System.out.println(lt1); //10:30:45LocalTime lt2 = LocalTime.of(10, 30, 45, 12345);System.out.println(lt2); //10:30:45.000012345 notice the left paddingLocalTime lt3 = LocalTime.ofInstant(Instant.now(), ZoneId.systemDefault());System.out.println(lt3); //15:48:58.358195我们可以使用
of()
方法创建LocalTime实例。 对于小时,分钟,秒和纳秒级的不同参数,有多种重载方法。 LocalTime lt = LocalTime.parse("10:30");System.out.println(lt); //10:30LocalTime lt1 = LocalTime.parse("10:30:45.1234");System.out.println(lt1); //10:30:45.123400LocalTime lt2 = LocalTime.parse("10.3.45.1234", DateTimeFormatter.ofPattern("H.m.s.n"));System.out.println(lt2); //10:03:45.000001234我们可以使用LocalTime parse()方法将String转换为LocalTime的实例。
There are methods in LocalTime class for different purposes. We have divided it into following categories.
LocalTime类中有一些用于不同目的的方法。 我们将其分为以下几类。
getHour()
, getMinute()
, getSecond()
and getNano()
. 从LocalTime获取时间– getHour()
, getMinute()
, getSecond()
和getNano()
。 plusHours()
, minusHours()
, plusMinutes()
, minusMinutes()
, plusSeconds()
, minusSeconds()
, plusNanos()
and minusNanos()
. 时间操纵– plusHours()
, minusHours()
, plusMinutes()
, minusMinutes()
, plusSeconds()
, minusSeconds()
, plusNanos()
和minusNanos()
。 isAfter()
and isBefore()
to check if this LocalTime is after/before the specified time. We can use these methods to compare two local times. isAfter()
比较– isAfter()
和isBefore()
以检查此isBefore()
是否在指定时间之后/之前。 我们可以使用这些方法来比较两个当地时间。 Let’s look at java LocalTime example program to explore all the above mentioned methods.
让我们看一下Java LocalTime示例程序,以探索上述所有方法。
package com.journaldev.java;import java.time.LocalTime;public class LocalTimeExample { public static void main(String[] args) { LocalTime lt = LocalTime.now(); System.out.println("Current Time: " + lt); System.out.println("\nHour: " + lt.getHour()); System.out.println("Minute: " + lt.getMinute()); System.out.println("Second: " + lt.getSecond()); System.out.println("Nanosecond: " + lt.getNano()); // add 2 hours, 10 minutes, 30 seconds and 1000 nanoseconds lt = lt.plusHours(2); lt = lt.plusMinutes(10); lt = lt.plusSeconds(30); lt = lt.plusNanos(1000); System.out.println("\nUpdated Time: " + lt); // minus 2 hours, 10 minutes, 30 seconds and 1000 nanoseconds lt = lt.minusHours(2); lt = lt.minusMinutes(10); lt = lt.minusSeconds(30); lt = lt.minusNanos(1000); System.out.println("\nUpdated Time: " + lt); LocalTime ct = LocalTime.now(); System.out.println("\nlt before ct ? " + lt.isBefore(ct)); System.out.println("lt after ct ? " + lt.isAfter(ct)); }}
That’s all for Java LocalTime example programs.
Java LocalTime示例程序就这些了。
Reference:
参考:
翻译自:
转载地址:http://iumzd.baihongyu.com/