Java时间工具类LocalTime

2023-12-29 20:28:17

嘚吧嘚

本篇文章整理一下有关LocalTime工具类的用法。个人感觉单独使用LocalTime类的几率不大🙃,使用频率最高的应该是LocalDateTime,但是既然都学习了,自然要全面一点🤨。

LocalTime - API

获取时间

函数声明描述
static LocalTime now()获取默认时区的当前时间
static LocalTime now(ZoneId zone)获取指定时区的当前时间
static LocalTime now(Clock clock)从指定时钟获取当前时间
static LocalTime of(int hour, int minute, int second)根据指定的时(0-23)、分(0-59)、秒(0-59)获取LocalTime实例
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// 获取当前时间
LocalTime now = LocalTime.now();
LocalTime time1 = LocalTime.now(ZoneId.of("America/New_York"));
LocalTime time2 = LocalTime.now(Clock.systemDefaultZone());
LocalTime time3 = LocalTime.of(19, 12, 5);

System.out.println("now : " + now.format(formatter) + " " + ZoneId.systemDefault());
System.out.println("time1 : " + time1.format(formatter) + " America/New_York");
System.out.println("time2 : " + time2.format(formatter));
System.out.println("time3 : " + time3.format(formatter));

在这里插入图片描述

获取时分秒

函数声明描述
int getHour()获取小时
int getMinute()获取分钟
int getSecond()获取秒钟
int getNano()获取纳秒
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// 获取当前时间
LocalTime time = LocalTime.of(19, 12, 5);
System.out.println("time : " + time.format(formatter));

System.out.println("getHour : " + time.getHour());
System.out.println("getMinute : " + time.getMinute());
System.out.println("getSecond : " + time.getSecond());
System.out.println("getNano : " + time.getNano());

在这里插入图片描述

增减时分秒

函数声明描述
LocalTime plusHours(long hoursToAdd)增加小时
LocalTime plusMinutes(long minutesToAdd)增加分钟
LocalTime plusSeconds(long secondstoAdd)增加秒
LocalTime plusNanos(long nanosToAdd)增加纳秒
LocalTime minusHours(long hoursToSubtract)减少小时
LocalTime minusMinutes(long minutesToSubtract)减少分钟
LocalTime minusSeconds(long secondsToSubtract)减少秒
LocalTime minusNanos(long nanosToSubtract)减少纳秒

plusXxx是增加时间,minusXxx是减少时间,但查看源码可以发现minusXxx还是用的plusXxx,所以这两类方法传参可为正数,也可为负数。正负号相同、数字相同时效果相反。
在这里插入图片描述
以小时和分钟为例

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// 获取当前时间
LocalTime time = LocalTime.of(19, 12, 5);
System.out.println("time : " + time.format(formatter));

System.out.println("plusHours : " + time.plusHours(10));
System.out.println("minusHours : " + time.minusHours(-10));

System.out.println("plusMinutes : " + time.plusMinutes(15));
System.out.println("minusMinutes : " + time.minusMinutes(-15));

在这里插入图片描述

替换时间

函数声明描述
LocalTime withHour(int hour)替换小时(0-23)
LocalTime withMinute(int minute)替换分钟(0-59)
LocalTime withSecond(int second)替换秒(0-59)
LocalTime withNano(int nanoOfSecond)替换纳秒(0-999999999)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// 获取当前时间
LocalTime time = LocalTime.of(19, 12, 5);
System.out.println("time : " + time.format(formatter));

System.out.println("withHour : " + time.withHour(10));
System.out.println("withMinute : " + time.withMinute(15));
System.out.println("withSecond : " + time.withSecond(26));
System.out.println("withNano : " + time.withNano(36));

在这里插入图片描述

比较时间

函数声明描述
int compareTo(LocalTime other)时间与另一个时间比较,x = y:0,x < y:-1,x > y::1
boolean isAfter(LocalTime other)检查时间是否在指定时间之后
boolean isBefore(LocalTime other)检查时间是否在指定时间之前
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// 获取当前时间
LocalTime time1 = LocalTime.of(19, 12, 5);
LocalTime time2 = LocalTime.of(18, 12, 5);
LocalTime time3 = LocalTime.of(20, 12, 5);
LocalTime time4 = LocalTime.of(19, 12, 5);

String timeStr1 = time1.format(formatter);
String timeStr2 = time2.format(formatter);
String timeStr3 = time3.format(formatter);
String timeStr4 = time4.format(formatter);
System.out.println("time1 : " + timeStr1);
System.out.println("time2 : " + timeStr2);
System.out.println("time3 : " + timeStr3);
System.out.println("time4 : " + timeStr4);
// 比较日期大小
System.out.println(timeStr1 + " = " + timeStr2 + " : " + time1.compareTo(time2));
System.out.println(timeStr1 + " = " + timeStr4 + " : " + time1.compareTo(time4));
System.out.println(timeStr1 + " = " + timeStr3 + " : " + time1.compareTo(time3));
System.out.println(timeStr1 + " > " + timeStr2 + " : " + time1.isAfter(time2));
System.out.println(timeStr1 + " < " + timeStr2 + " : " + time1.isBefore(time2));

在这里插入图片描述
欢迎补充😉。

文章来源:https://blog.csdn.net/engerla/article/details/135292612
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。