📜  Java8 LocalTime类

📅  最后修改于: 2020-10-01 06:37:04             🧑  作者: Mango

Java LocalTime类

Java LocalTime类是一个不可变的类,它以小时-分钟-秒的默认格式表示时间。它继承了Object类并实现Comparable接口。

Java LocalTime类声明

让我们看看java.time.LocalTime类的声明。

public final class LocalTime extends Object 
implements Temporal, TemporalAdjuster, Comparable, Serializable

Java LocalTime方法

Method Description
LocalDateTime atDate(LocalDate date) It is used to combine this time with a date to create a LocalDateTime.
int compareTo(LocalTime other) It is used to compare this time to another time.
String format(DateTimeFormatter formatter) It is used to format this time using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this time as an int.
LocalTime minusHours(long hoursToSubtract) It is used to return a copy of this LocalTime with the specified number of hours subtracted.
LocalTime minusMinutes(long minutesToSubtract) It is used to return a copy of this LocalTime with the specified number of minutes subtracted.
static LocalTime now() It is used to obtain the current time from the system clock in the default time-zone.
static LocalTime of(int hour, int minute, int second) It is used to obtain an instance of LocalTime from an hour, minute and second.
LocalTime plusHours(long hoursToAdd) It is used to return a copy of this LocalTime with the specified number of hours added.
LocalTime plusMinutes(long minutesToAdd) It is used to return a copy of this LocalTime with the specified number of minutes added.

Java LocalTime示例:now()

import java.time.LocalTime;
public class LocalTimeExample1 {
  public static void main(String[] args) {
    LocalTime time = LocalTime.now();
    System.out.println(time);
  }
}

输出:

15:19:47.459

Java LocalTime示例:of()

import java.time.LocalTime;
public class LocalTimeExample2 {
  public static void main(String[] args) {
    LocalTime time = LocalTime.of(10,43,12);
    System.out.println(time);
  }
}

输出:

10:43:12

Java LocalTime示例:minusHours()和minusMinutes()

import java.time.LocalTime;
public class LocalTimeExample3 {
  public static void main(String[] args) {
    LocalTime time1 = LocalTime.of(10,43,12);
    System.out.println(time1);
    LocalTime time2=time1.minusHours(2);
    LocalTime time3=time2.minusMinutes(34);
    System.out.println(time3);
  }
}

输出:

10:43:12
08:09:12

Java LocalTime示例:plusHours()和plusMinutes()

import java.time.LocalTime;
public class LocalTimeExample4 {
  public static void main(String[] args) {
    LocalTime time1 = LocalTime.of(10,43,12);
    System.out.println(time1);
    LocalTime time2=time1.plusHours(4);
    LocalTime time3=time2.plusMinutes(18);
    System.out.println(time3);
  }
}

输出:

10:43:12
15:01:12

Java LocalTime示例

import java.time.*;
import java.time.temporal.ChronoUnit;
public class LocalTimeExample5 {
  public static void main(String... args) {
    ZoneId zone1 = ZoneId.of("Asia/Kolkata");
    ZoneId zone2 = ZoneId.of("Asia/Tokyo");
    LocalTime time1 = LocalTime.now(zone1);
    System.out.println("India Time Zone: "+time1);
    LocalTime time2 = LocalTime.now(zone2);
    System.out.println("Japan Time Zone: "+time2);
    long hours = ChronoUnit.HOURS.between(time1, time2);
    System.out.println("Hours between two Time Zone: "+hours);
    long minutes = ChronoUnit.MINUTES.between(time1, time2);
    System.out.println("Minutes between two time zone: "+minutes);
  }
}

输出:

India Time Zone: 14:56:43.087
Japan Time Zone: 18:26:43.103
Hours between two Time Zone: 3
Minutes between two time zone: 210