Java8学习笔记 — 新日期时间API

来源:互联网 发布:经纬度定位软件ios 编辑:程序博客网 时间:2024/06/08 09:34

LocalDate、LocalTime、LocalDateTime类的实例是不可变的对象,分别表示使用ISO8601日历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。

@Testpublic void test1() {LocalDateTime ldt = LocalDateTime.now();System.out.println(ldt);LocalDateTime ldt2 = LocalDateTime.of(2017, 05, 24, 14, 18, 30);System.out.println(ldt2);System.out.println(ldt.plusYears(2));System.out.println(ldt.minusYears(2));System.out.println(ldt.getYear());System.out.println(ldt.getMonth());System.out.println(ldt.getDayOfMonth());System.out.println(ldt.getDayOfYear());System.out.println(ldt.getMinute());}


Instant:时间戳(以Unix元年:1970年1月1日00:00:00到某个时间之间的毫秒值)

@Testpublic void test2() {Instant ins1 = Instant.now(); // 默认获取UTC时区System.out.println(ins1);OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(8));System.out.println(odt);System.out.println(ins1.toEpochMilli());Instant ins2 = Instant.ofEpochMilli(1000);System.out.println(ins2);}


DurationPeriod:计算两个时间间隔

@Testpublic void test4() {LocalDate ld1 = LocalDate.of(2016, 10, 10);LocalDate ld2 = LocalDate.now();Period period = Period.between(ld1, ld2);System.out.println(period);System.out.println(period.getYears());System.out.println(period.getMonths());System.out.println(period.getDays());}@Testpublic void test3() {Instant ins1 = Instant.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}Instant ins2 = Instant.now();Duration duration = Duration.between(ins1, ins2);System.out.println(duration.toMillis());System.out.println("-------------------");LocalTime lt1 = LocalTime.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}LocalTime lt2 = LocalTime.now();System.out.println(Duration.between(lt1, lt2).toMillis());}


TemporalAdjuster:时间校正器

TemporalAdjusters:该类通过静态方法提供了大量的常用TemporalAdjuster的实现

@Testpublic void test5() {LocalDateTime ldt = LocalDateTime.now();System.out.println(ldt);LocalDateTime ldt2 = ldt.withDayOfMonth(18);System.out.println(ldt2);LocalDateTime ldt3 = ldt.with(TemporalAdjusters.firstDayOfMonth());System.out.println(ldt3);LocalDateTime ldt4 = ldt.with(TemporalAdjusters.firstDayOfNextYear());System.out.println(ldt4);System.out.println("---------------------");System.out.println(ldt.withMonth(3));System.out.println(ldt.withMinute(12));System.out.println(ldt.withHour(5));System.out.println("---------------------");System.out.println(ldt.with(TemporalAdjusters.lastDayOfMonth()));System.out.println(ldt.with(TemporalAdjusters.lastDayOfYear()));System.out.println(ldt.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));System.out.println(ldt.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY)));}


DateTimeFormatter:日期时间格式化

@Testpublic void test6() {DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;LocalDateTime ldt = LocalDateTime.now();String str = ldt.format(dtf);System.out.println(str);DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String str2 = ldt.format(dtf2);System.out.println(str2);LocalDateTime newDate = ldt.parse(str2, dtf2);System.out.println(newDate);}


ZoneTimeZoneDateZoneDateTime:时区处理

每个时区都对应着ID,地区ID都为“{区域}/{城市}”的格式,例如:Aisa/Shanghai

ZoneId:该类中包含了所有的时区信息

         getAvailableZoneIds():可以获取所有时区信息

         of(String id):用指定的时区信息获取ZoneId对象

@Testpublic void test8() {LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Pacific/Majuro"));System.out.println(ldt);LocalDateTime ldt2 = LocalDateTime.now(Clock.systemDefaultZone());System.out.println(ldt2);LocalDateTime ldt3 = LocalDateTime.now(ZoneId.of("America/Los_Angeles"));ZonedDateTime zdt = ldt3.atZone(ZoneId.of("Asia/Shanghai"));System.out.println(zdt);}@Testpublic void test7() {Set<String> zoneIds = ZoneId.getAvailableZoneIds();zoneIds.forEach(System.out::println);}



原创粉丝点击