LocalTime

This commit is contained in:
seahi 2024-11-13 21:38:29 +08:00
parent 78135ec799
commit 4d4a3beb6a

View File

@ -0,0 +1,30 @@
package chapter5;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Example22 {
public static void main(String[] args) {
// 1. 获取当前时间
LocalTime now = LocalTime.now();
// 2. 表示过去时间
LocalTime of = LocalTime.of(9, 23, 23);
// 3. 三个getter
System.out.println("时:" + now.getHour());
System.out.println("分:" + now.getMinute());
System.out.println("秒:" + now.getSecond());
// 4. 格式转换借助 DateTimeFormatter
System.out.println("\nnow默认格式" + now);
System.out.println("不显示微秒:" + now.withNano(0));
System.out.println("转换格式:" + now.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
System.out.println("转换格式:" + now.format(DateTimeFormatter.ofPattern("HH点mm分ss秒")));
// 5. 判断
System.out.println("\nof在now之前吗" + of.isBefore(now));
// 6. 解析字符串
LocalTime time = LocalTime.parse("12:15:30");
System.out.println("将字符串转换成时间对象:" + time);
}
}