提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用LocalTime类获取并显示当前时间。
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; /**来自 N o w J a v a . c o m - 时代Java**/ public class Main { public static void main(String[] args){ LocalTime time = LocalTime.now(); System.out.println("Current Time: " + time); // atDate(LocalDate): obtain the local date and time LocalDateTime ldt = time.atDate(LocalDate.of(2011,Month.NOVEMBER,11)); System.out.println("Local Date Time object: " + ldt); // of(int hours, int min): obtain a specific time LocalTime pastTime = LocalTime.of(1, 10); // compareTo(LocalTime): compare two times. Positive // return value returned if greater System.out.println("Comparing times: " + time.compareTo(pastTime));/** NowJava.com - 时 代 Java 提 供 **/ // getHour(): return hour in int value (24-hour format) int hour = time.getHour(); int min = time.getMinute(); int nano = time.getNano(); int sec = time.getSecond(); System.out.println("Hour: " + hour); // isAfter(LocalTime): return Boolean comparison System.out.println("Is local time after pastTime? " + time.isAfter(pastTime)); // minusHours(int): Subtract Hours from LocalTime