jdk8在java.time包下新增了一批时间api,使用起来比老的Date更方便;
java.time包下的内容:
常用的LocalDate、LocalTime、LocalDateTime,其他基本都是辅助用,LocalDate只用于年月日,LocalTime只用于时分秒,LocalDateTime可用于年月日时分秒,这三个类的方法都比较相似;
可以通过now()方法获得系统当前时间:
@Test
public void test() {
LocalDate ld = LocalDate.now();
System.out.println(ld);
LocalTime lt = LocalTime.now();
System.out.println(lt);
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
ldt = LocalDateTime.now(Clock.systemDefaultZone());
System.out.println(ldt);
ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println(ldt);
// ZoneId.getAvailableZoneIds().stream().forEach(System.out::println);//查看所有可用的时区
}
也可以直接指定时间,或者自定义格式创建:
@Test
public void test() {
System.out.println("[LocalDate]");
LocalDate ld = LocalDate.of(2000, 6, 6);
System.out.println(ld);
ld = LocalDate.of(2000, Month.JUNE, 6);
System.out.println(ld);
ld = LocalDate.ofEpochDay(32L);//0是1970-1-1
System.out.println(ld);
ld = LocalDate.ofYearDay(1970, 32);
System.out.println(ld);
System.out.println("[LocalTime]");
LocalTime lt = LocalTime.of(10, 20);
System.out.println(lt);
lt = LocalTime.of(10, 20, 30);
System.out.println(lt);
lt = LocalTime.of(10, 20, 30, 123456);
System.out.println(lt);
lt = LocalTime.ofNanoOfDay(60 * 1000000000L);
System.out.println(lt);
lt = LocalTime.ofSecondOfDay(60L);
System.out.println(lt);
System.out.println("[LocalDateTime]");
LocalDateTime ldt = LocalDateTime.of(ld, lt);
System.out.println(ldt);
ldt = LocalDateTime.of(2022, 2, 22, 22, 22);
System.out.println(ldt);
ldt = LocalDateTime.of(2022, Month.FEBRUARY, 22, 22, 22);
System.out.println(ldt);
ldt = LocalDateTime.of(1999, 3, 4, 5, 6, 7);
System.out.println(ldt);
ldt = LocalDateTime.of(1999, Month.MARCH, 4, 5, 6, 7);
System.out.println(ldt);
ldt = LocalDateTime.of(2000, 1, 10, 20, 30, 40, 50);
System.out.println(ldt);
ldt = LocalDateTime.of(2000, Month.JANUARY, 10, 20, 30, 40, 50);
System.out.println(ldt);
ldt = LocalDateTime.ofEpochSecond(62, 70, ZoneOffset.ofHours(8));
System.out.println(ldt);
ldt = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
System.out.println(ldt);
System.out.println("=========================");
System.out.println(LocalDate.from(ldt));
System.out.println(LocalTime.from(ldt));
System.out.println(LocalDateTime.from(ldt));
System.out.println(LocalDate.parse("1999-01-01"));//格式固定为 xxxx-xx-xx (DateTimeFormatter.ISO_LOCAL_DATE的格式)
System.out.println(LocalTime.parse("12:34:56"));//格式为 xx:xx 或者 xx:xx:xx (DateTimeFormatter.ISO_LOCAL_TIME的格式)
System.out.println(LocalDateTime.parse("1999-01-01T12:34"));//Date格式和Time格式中间用字母T连接(DateTimeFormatter.ISO_LOCAL_DATE_TIME的格式)
System.out.println(LocalDate.parse("自定义2222年,02月,22日13:14:15:666666666", DateTimeFormatter.ofPattern("自定义yyyy年,MM月,dd日HH:mm:ss:SSSSSSSSS")));//只识别日期
System.out.println(LocalTime.parse("自定义2222年,02月,22日13:14:15:666", DateTimeFormatter.ofPattern("自定义yyyy年,MM月,dd日HH:mm:ss:SSS")));//只识别时间
System.out.println(LocalDateTime.parse("自定义2222年,02月,22日13:14:15:666666666", DateTimeFormatter.ofPattern("自定义yyyy年,MM月,dd日HH:mm:ss:SSSSSSSSS")));
}
可对日期时间进行操作,3个类的方法基本类似,常用的方法:
@Test
public void test() {
LocalDateTime ldt = LocalDateTime.of(1999, 1, 2, 3, 4, 5, 6);
System.out.println(ldt);
System.out.println(ldt.getYear());
System.out.println(ldt.getHour());
System.out.println(ldt.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH点mm分ss秒")));
LocalDateTime newldt = ldt.withYear(2000).withHour(23).withSecond(59);
System.out.println(ldt);
System.out.println(newldt);
System.out.println(ldt.isBefore(newldt));
newldt = ldt.plusYears(0).plusMonths(13).plusDays(27).minusHours(2);
System.out.println(newldt);
newldt = ldt.plusYears(1).plusMonths(13).plusDays(27).minusHours(2);
System.out.println(newldt);
System.out.println(ldt.toLocalDate());
}
Instant用于记录某一时刻,跟Date差不多,两者可以互转,功能比Date多:
@Test
public void test() {
Instant instant = Instant.now();
System.out.println(instant);
Date d = Date.from(instant);
System.out.println(d);
System.out.println(d.toInstant());
System.out.println(Instant.ofEpochMilli(60 * 60 * 1000L));
System.out.println(Instant.parse("2000-11-11T01:02:03.456789Z"));//格式为 xxxx-xx-xxTxx:xx:xx.xxxZ
Instant newInstant = instant.plusSeconds(3600L);
System.out.println(newInstant);
newInstant = instant.minusMillis(10 * 1000L);
System.out.println(newInstant);
System.out.println(instant.isBefore(newInstant));
}
Duration可以用于计算时间间隔(时分秒),不能用于LocalDate,计算LocalDate之间的日期间隔需要使用Period:
@Test
public void test() {
LocalDateTime ldt1 = LocalDateTime.of(2000, 1, 1, 1, 1, 1);
LocalDateTime ldt2 = LocalDateTime.of(2000, 1, 2, 2, 1, 1);
Duration duration = Duration.between(ldt1, ldt2);
System.out.println(duration);
LocalTime lt1 = LocalTime.of(1, 1);
LocalTime lt2 = LocalTime.of(2, 2);
Duration d = Duration.between(lt1, lt2);
System.out.println(d);
System.out.println(d.toMinutes());
System.out.println(d.getSeconds());
}
@Test
public void test() {
LocalDate ld1 = LocalDate.of(1999, 1, 1);
LocalDate ld2 = LocalDate.of(2001, 4, 6);
Period p = Period.between(ld1, ld2);
System.out.println(p);
System.out.println(p.getYears());
System.out.println(p.getMonths());
System.out.println(p.getDays());
System.out.println(p.toTotalMonths());
}
还有一些其他的类:
@Test
public void test() {
MonthDay md = MonthDay.now();
System.out.println(md);
Year y = Year.now();
System.out.println(y);
YearMonth ym = YearMonth.now();
System.out.println(ym);
ym = YearMonth.of(1, 2);
System.out.println(ym);
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(zdt);
ZoneOffset zo = ZoneOffset.of("+8");
System.out.println(zo);
zo = ZoneOffset.ofHoursMinutes(8, 30);
System.out.println(zo);
OffsetDateTime odt = OffsetDateTime.now();
System.out.println(odt);
OffsetTime ot = OffsetTime.now();
System.out.println(ot);
ot = OffsetTime.now(ZoneId.of("+9"));
System.out.println(ot);
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/FlyLikeButterfly/article/details/126123024
内容来源于网络,如有侵权,请联系作者删除!