📜  Java的.time.LocalTime类在Java中

📅  最后修改于: 2022-05-13 01:55:46.678000             🧑  作者: Mango

Java的.time.LocalTime类在Java中

Java是最流行的编程语言和广泛使用的编程语言。 Java用于各种应用程序,如移动应用程序、桌面应用程序、Web 应用程序。在Java, Java.time.LocalTime 类表示时间,它被视为时-分-秒。这个类是不可变的,也是线程安全的。 Java.time.LocalTime 类下有各种方法,下面解释,下面也进行实现。 Java.time是一个用于处理当前日期和时间 API 的包。

  • 导入Java.time.LocalTime 等类。
  • 现在使用 LocalTime.now() 来存储当前时间。
  • 显示存储在变量中的当前时间。

首先,这个类的所有方法都以表格格式展示如下:

MethodDescription
adjustInto(Temporal temporal)

All MethodsStatic MethodsInstance MethodsConcrete Methods

Modifier and Type Method and Description

Temporal



atDate(LocalDate date)Combines this time with a date to create a LocalDateTime.
atOffset(ZoneOffset offset)Combines this time with an offset to create an OffsetTime.
compareTo(LocalTime other)Compares this time to another time.
equals(Object obj)Checks if this time is equal to another time.
format(DateTimeFormatter formatter)Formats this time using the specified formatter.
get(TemporalField field)Gets the value of the specified field from this time as an int.
getHour()Gets the hour-of-day field.
getMinute()Gets the minute-of-hour field.
getNano()Gets the nano-of-second field.
getSecond()Gets the second-of-minute field.
hashCode()A hash code for this time.
isAfter(LocalTime other)Checks if this time is after the specified time.
isBefore(LocalTime other)Checks if this time is before the specified time.
isSupported(TemporalField field)Checks if the specified field is supported.
minus(long amountToSubtract, TemporalUnit unit)Returns a copy of this time with the specified amount subtracted.
minusHours(long hoursToSubtract)Returns a copy of this LocalTime with the specified number of hours subtracted.
minusMinutes(long minutesToSubtract)Returns a copy of this LocalTime with the specified number of minutes subtracted.
minusNanos(long nanosToSubtract)Returns a copy of this LocalTime with the specified number of nanoseconds subtracted.
minusSeconds(long secondsToSubtract)Returns a copy of this LocalTime with the specified number of seconds subtracted.
 now()Obtains the current time from the system clock in the default time-zone.
of(int hour, int minute)Obtains an instance of LocalTime from an hour and minute.
ofNanoOfDay(long nanoOfDay)Obtains an instance of LocalTime from a nanos-of-day value.
ofSecondOfDay(long secondOfDay)Obtains an instance of LocalTime from a second-of-day value.
parse(CharSequence text)Obtains an instance of LocalTime from a text string such as 10:15.
plus(TemporalAmount amountToAdd)Returns a copy of this time with the specified amount added.
plusHours(long hoursToAdd)Returns a copy of this LocalTime with the specified number of hours added.
plusMinutes(long minutesToAdd)Returns a copy of this LocalTime with the specified number of minutes added.
plusNanos(long nanosToAdd)Returns a copy of this LocalTime with the specified number of nanoseconds added.
plusSeconds(long secondstoAdd)Returns a copy of this LocalTime with the specified number of seconds added.
query(TemporalQuery query)Queries this time using the specified query.
range(TemporalField field)Gets the range of valid values for the specified field.
toNanoOfDay()Extracts the time as nanos of day, from 0 to 24 * 60 * 60 * 1,000,000,000 – 1.
truncatedTo(TemporalUnit unit)Returns a copy of this LocalTime with the time truncated.
with(TemporalAdjuster adjuster)Returns an adjusted copy of this time.
withHour(int hour)Returns a copy of this LocalTime with the hour-of-day altered.
withMinute(int minute)Returns a copy of this LocalTime with the minute-of-hour altered.
withNano(int nanoOfSecond)Returns a copy of this LocalTime with the nano-of-second altered.
withSecond(int second)Returns a copy of this LocalTime with the second-of-minute altered.

执行:

示例 1

Java
// Java Program to illustrate LocalTime Class
 
// Importing LocalDateTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class  GFG {
   
    // Main driver method
    public static void main(String args[]) {
       
      // Creating an object of LocalTime class by
      // declaring a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
       
      // Print and display the LocalTime value
        System.out.println("Local time value : "+ltime);       
    }
}


Java
// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
    // main driver method
    public static void main(String args[])
    {
        // Creating an object of LocalTime Class by
        // declaring a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
         
      // Now plusMinute() will add up minutes to the LocalTime
        LocalTime time3 = ltime.plusMinutes(120);
         
        // Display the time using object of class
        System.out.println("Time : "+time3);
    }
}


Java
// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
   // Main class
    public static void main(String args[])
    {
        // Creating an object of LocalTime class by
        // declare a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
       
        // plusHours() method will add up Hours to
        // the LocalTime
        LocalTime time3 = ltime.plusHours(05);
         
        // Display the time
        System.out.println(time3);
    }
}


Java
// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
    // Main driver method
    public static void main(String args[])
    {
        // creating an object of. LocalTime Class by
        // declare a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
       
        // minusMinute() will subtract minutes from
        // the LocalTime
        LocalTime time = ltime.minusMinutes(120);
       
        // Display the time
        System.out.println(time);
    }
}


输出
Local time value : 06:20:52.986897

示例 2

Java

// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
    // main driver method
    public static void main(String args[])
    {
        // Creating an object of LocalTime Class by
        // declaring a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
         
      // Now plusMinute() will add up minutes to the LocalTime
        LocalTime time3 = ltime.plusMinutes(120);
         
        // Display the time using object of class
        System.out.println("Time : "+time3);
    }
}
输出
Time : 08:34:59.135137

示例 3

Java

// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
   // Main class
    public static void main(String args[])
    {
        // Creating an object of LocalTime class by
        // declare a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
       
        // plusHours() method will add up Hours to
        // the LocalTime
        LocalTime time3 = ltime.plusHours(05);
         
        // Display the time
        System.out.println(time3);
    }
}
输出
17:48:53.003478

示例 4

Java

// Java Program to illustrate LocalTime
// where plusMethod() is illustrated
 
// Importing LocalTime class from
// java.time package
import java.time.LocalTime;
 
// Main Class
public class GFG {
   
    // Main driver method
    public static void main(String args[])
    {
        // creating an object of. LocalTime Class by
        // declare a variable to store LocalTime
        LocalTime ltime = LocalTime.now();
       
        // minusMinute() will subtract minutes from
        // the LocalTime
        LocalTime time = ltime.minusMinutes(120);
       
        // Display the time
        System.out.println(time);
    }
}
输出
10:54:49.649675