文章16 | 阅读 6560 | 点赞0
LocalTime 时java8 中用于表示时间的类.
package org.zongf.learn.java8.date;
import org.junit.Test;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
/** * @Description: 测试LocalTime 类 * @author: zongf * @date: 2018-06-29 15:22 */
public class TestLocalTime {
// 构造方法
@Test
public void test_constructor(){
// 获取当前时间,默认格式: HH:mm:ss.SSS
System.out.println(LocalTime.now());
// 通过时分创建时间
System.out.println(LocalTime.of(10, 30));
// 通过时分秒创建时间
System.out.println(LocalTime.of(10, 30, 50));
// 通过时分秒毫秒创建时间
System.out.println(LocalTime.of(10, 30, 50, 459));
// 获取当天第1000 秒的时间
System.out.println(LocalTime.ofSecondOfDay(1000));
// 获取当天第 1000 纳秒的时间
System.out.println(LocalTime.ofNanoOfDay(1000000000));
// 通过字符串解析时间
System.out.println(LocalTime.parse("10:30"));
System.out.println(LocalTime.parse("10:30:50"));
System.out.println(LocalTime.parse("10:30:50.361"));
// 通过自定义格式解析时间
System.out.println(LocalTime.parse("10:30:50.459", DateTimeFormatter.ofPattern("HH:mm:ss.SSS")));
}
// 测试get 方法
@Test
public void test_get(){
LocalTime now = LocalTime.now();
System.out.println("当前时间:" + now);
System.out.println("时:" + now.getHour());
System.out.println("分:" + now.getMinute());
System.out.println("秒:" + now.getSecond());
System.out.println("毫秒:" + now.getNano());
System.out.println("当天第几秒:" + now.toSecondOfDay());
System.out.println("当天第几毫秒:" + now.toNanoOfDay());
System.out.println("时:" + now.get(ChronoField.HOUR_OF_DAY));
System.out.println("分:" + now.get(ChronoField.MINUTE_OF_HOUR));
System.out.println("秒:" + now.get(ChronoField.SECOND_OF_MINUTE));
System.out.println("毫秒:" + now.get(ChronoField.MILLI_OF_SECOND));
System.out.println("微妙:" + now.getLong(ChronoField.MICRO_OF_SECOND));
System.out.println("纳秒:" + now.get(ChronoField.NANO_OF_SECOND));
System.out.println("当天第几分钟:" + now.get(ChronoField.MINUTE_OF_DAY));
System.out.println("当天第几秒:" + now.get(ChronoField.SECOND_OF_DAY));
System.out.println("当天第几毫秒:" + now.get(ChronoField.MILLI_OF_DAY));
System.out.println("当天第几微妙:" + now.getLong(ChronoField.MICRO_OF_DAY));
System.out.println("当天第几纳秒:" + now.getLong(ChronoField.NANO_OF_DAY));
System.out.println("12小时制,取值范围[0,11],时:" + now.get(ChronoField.HOUR_OF_AMPM));
System.out.println("12小时制,取值范围[1,12],时:" + now.get(ChronoField.CLOCK_HOUR_OF_AMPM));
System.out.println("24小时制,取值范围[1,24],时:" + now.get(ChronoField.CLOCK_HOUR_OF_DAY));
}
// 测试时间运算方法
@Test
public void test_plus(){
LocalTime lt = LocalTime.of(13,30,40, 100_000_000);
System.out.println("当前时间:" + lt);
System.out.println();
System.out.println("3小时后:" + lt.plusHours(3));
System.out.println("20分钟后:" + lt.plusMinutes(20));
System.out.println("10秒后:" + lt.plusSeconds(10));
System.out.println("2_000_000纳秒后:" + lt.plusNanos(2_000_000));
System.out.println("3小时前:" + lt.minusHours(3));
System.out.println("20分钟前:" + lt.minusMinutes(20));
System.out.println("10秒钟前:" + lt.minusSeconds(10));
System.out.println("2_000_000秒钟前:" + lt.minusNanos(2_000_000));
System.out.println();
System.out.println("3小时后:" + lt.plus(Duration.ofHours(3)));
System.out.println("20分钟后:" + lt.plus(Duration.ofMinutes(20)));
System.out.println("10秒钟后:" + lt.plus(Duration.ofSeconds(10)));
System.out.println("2000毫秒后:" + lt.plus(Duration.ofMillis(200)));
System.out.println("2_000_000纳秒后:" + lt.plus(Duration.ofNanos(2_000_000)));
System.out.println();
System.out.println("3小时前:" + lt.minus(Duration.ofHours(3)));
System.out.println("20分钟前:" + lt.minus(Duration.ofMinutes(20)));
System.out.println("10秒钟前:" + lt.minus(Duration.ofSeconds(10)));
System.out.println("2000毫秒前:" + lt.minus(Duration.ofMillis(200)));
System.out.println("2_000_000纳秒前:" + lt.minus(Duration.ofNanos(2_000_000)));
System.out.println();
}
// 测试比较方法
@Test
public void compare(){
LocalTime lt1 = LocalTime.of(10, 30, 50, 100_000_000);
LocalTime lt2 = LocalTime.of(16, 20, 30, 200_000_000);
System.out.println(lt1.isBefore(lt2));
System.out.println(lt1.isAfter(lt2));
System.out.println(lt1.compareTo(lt2));
System.out.println(lt2.compareTo(lt1));
}
// 测试修改方法
@Test
public void with(){
LocalTime lt = LocalTime.of(10, 30, 50, 100_000_000);
System.out.println(lt);
System.out.println("替换小时为15: " + lt.withHour(15));
System.out.println("替换分钟为35: " + lt.withMinute(35));
System.out.println("替换秒为55: " + lt.withSecond(55));
System.out.println("替换纳秒为300_000_000: " + lt.withNano(300_000_000));
}
// 测试时间间隔方法
@Test
public void test_until(){
// 计算两个时间之间的差
LocalTime ldt1 = LocalTime.of(18, 10, 20, 100_000);
LocalTime ldt2 = LocalTime.of(8, 30, 40, 200_000);
System.out.println("时间间隔-总小时数:" + ldt1.until(ldt2, ChronoUnit.HOURS));
System.out.println("时间间隔-总分钟数:" + ldt1.until(ldt2, ChronoUnit.MINUTES));
System.out.println("时间间隔-总秒数:" + ldt1.until(ldt2, ChronoUnit.SECONDS));
System.out.println("时间间隔-总毫秒数:" + ldt1.until(ldt2, ChronoUnit.MILLIS));
System.out.println("时间间隔-总微秒数:" + ldt1.until(ldt2, ChronoUnit.MICROS));
System.out.println("时间间隔-总纳秒数:" + ldt1.until(ldt2, ChronoUnit.NANOS));
System.out.println("时间间隔-总半天数(多少个12小时):" + ldt1.until(ldt2, ChronoUnit.HALF_DAYS));
}
// 计算时间间隔
@Test
public void test_between(){
LocalTime lt1 = LocalTime.of(12, 30, 50, 100_000_000);
LocalTime lt2 = LocalTime.of(15, 50, 30, 200_000_000);
Duration duration = Duration.between(lt1, lt2);
System.out.println("时间间隔-总小时数:" + duration.toHours());
System.out.println("时间间隔-总分钟数:" + duration.toMinutes());
System.out.println("时间间隔-总秒数:" + duration.toMillis());
System.out.println("时间间隔-总毫秒数:" + duration.toNanos());
System.out.println("时间间隔-总纳秒数:" + duration.toString());
System.out.println("日期间隔是否是负数:" + duration.isNegative());
System.out.println("日期间隔是否是0:" + duration.isZero());
}
// 测试转换为LocalDateTime 方法
@Test
public void test_converter(){
LocalTime lt = LocalTime.now();
LocalDateTime ldt = lt.atDate(LocalDate.now());
System.out.println(ldt);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://zongf.blog.csdn.net/article/details/90057782
内容来源于网络,如有侵权,请联系作者删除!