嘚吧嘚
本篇文章整理一下有关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));
欢迎补充😉。