java8在日常开发中使用LocalDate和LocalTime

首页 / 新闻资讯 / 正文

  1. 原先的Date and Calendar 类的api比较复杂,不易于理解,应用起来不是很灵活。
  2. Calendar 是个线程不安全的类会导致SimpleDateFormat线程不安全。
  3. java.time是JSR 310: Date and Time API.规范所开发,其所有类都是线程安全的或者是枚举类型的类
  4. java.time 的API 使用简单,能够灵活计算时间,矫正时间。

LocalDate 是 日期,在java.time 中 日期和时间是可以分开和组合的。

2.1 创建date的方式

 // 创建date的方式     @Test     public void LocalDateTest(){         // 1当前日期 常用         LocalDate now = LocalDate.now();         System.out.println(now);//2019-10-27         // 2指定年月 日 方式 常用         LocalDate ofDate = LocalDate.of(2018, 8, 8);         System.out.println(ofDate);//2018-08-08         // 3使用Clock方式创建 不常用         Clock clock = Clock.systemDefaultZone();         LocalDate date = LocalDate.now(clock);         System.out.println(date);// 2019-10-27         // 4 指定年份 和 一年的天数进行创建         LocalDate localDate = LocalDate.ofYearDay(2018, 256);         System.out.println(localDate);// 2018-09-13              }

2.2 使用LocalDate读取date

 @Test     public void LocalDateTest2(){         // 创建时间         LocalDate date = LocalDate.of(2019,10,27);         // 获得年份 2019         date.getYear();         System.out.println(date.getYear());         // 获得一个月中的第几天 27         date.getDayOfMonth();         System.out.println(date.getDayOfMonth());         // 获得星期 SUNDAY         date.getDayOfWeek();         System.out.println(date.getDayOfWeek());         // 获得一年中的第几天 300         date.getDayOfYear();         System.out.println(date.getDayOfYear());         // 获得月份值 10         date.getMonthValue();         System.out.println(date.getMonthValue());         // 获得月份长度 31         date.lengthOfMonth();         System.out.println(date.lengthOfMonth());         // 是否是闰年 false         date.isLeapYear();         System.out.println(date.isLeapYear());      }

2.3 TemporalField 读取 LocalDate 的值

ChronoField 是个枚举其实现了TemporalField接口,除了2.2的方式读取date我们还可以使用ChronoField方式读取date.

    // 使用 TemporalField 读取 LocalDate 的值     @Test     public void LocalDateTest3() {         // 创建时间         LocalDate date = LocalDate.of(2019, 10, 27);         // 获得年份 2019         date.get(ChronoField.YEAR);         System.out.println(date.get(ChronoField.YEAR));         // 获得月份 10         date.get(ChronoField.MONTH_OF_YEAR);         System.out.println(date.get(ChronoField.MONTH_OF_YEAR));         // 获得这个月中的第几天 27         date.get(ChronoField.DAY_OF_MONTH);         System.out.println(date.get(ChronoField.DAY_OF_MONTH));         // 获得这个星期的第几天 7         date.get(ChronoField.DAY_OF_WEEK);         System.out.println(date.get(ChronoField.DAY_OF_WEEK));         // 其他不再举例自行研究都是字面意思很好理解      }

2.4 解析LocalDate

   @Test     public void LocalDateParse(){         // 默认支持格式解析         String dateStr = "2019-10-27";         LocalDate parse = LocalDate.parse(dateStr);         System.out.println(parse);//2019-10-27         // 指定格式解析         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");         LocalDate date = LocalDate.parse("2019/10/27", dateTimeFormatter);         System.out.println(date);//2019-10-27     }

2.5 使用 Period 操纵 date

获得连个日期之间的差值,可以获得年,月,日,判断是否非0等等。

    @Test     public void LocalDatePor(){         LocalDate date1 = LocalDate.of(2019, 10, 27);         LocalDate date2 = LocalDate.of(2019, 10, 25);         Period between = Period.between(date2, date1);         System.out.println(between.getDays());// 2     }

2.6 修改date

通过withAttribute修改不会改变原来的date,会在原来date的基础上形成新的LocalDate副本。

    // 修改     @Test     public void LocalDateWith(){         LocalDate date1 = LocalDate.of(2019, 10, 27);         LocalDate date2 = date1.withMonth(9);//2019-09-27         System.out.println(date2);         LocalDate date3 = date2.withYear(2018);//2018-09-27         System.out.println(date3);         // 2019-10-27         System.out.println(date1);     }

2.7 使用 TemporalAdjuster 修改日期

TemporalAdjuster 时间矫正器修改时间也是不会改变原来的date,会新生成LocalDate 副本,相比于withAttribute,其API更加丰富,提供大量的静态工厂方法,能满足我们日常开发需求。

    //  TemporalAdjuster youku1327     @Test     public void LocalDateTemporalAdjuster(){         LocalDate date1 = LocalDate.of(2019, 10, 27);         LocalDate date2 = date1.with(TemporalAdjusters.firstDayOfMonth());         // 2019-10-01         System.out.println(date2);         LocalDate date3 = date1.with(TemporalAdjusters.firstDayOfYear());         // 2019-01-01         System.out.println(date3);         LocalDate date4 = date1.with(TemporalAdjusters.lastDayOfYear());         // 2019-12-31         System.out.println(date4);      }

如果你已经掌握了上述的LocalDate的基本用法,那么学习LocalTime也是十分简单,原因是LocalTime和LocalDate的API基本差不多。

3.1 创建LocalTime

    @Test     public void localTimeTest1(){         // 1当前时间         LocalTime now = LocalTime.now();         System.out.println(now);//22:49:03.360         // 2指定时间         LocalTime of = LocalTime.of(22, 47);         System.out.println(of);//22:47      }

3.2 读取时间

@Test     public void localTimeRead(){         // 1指定时间         LocalTime tiem = LocalTime.of(22, 50);         // 小时         int hour = tiem.getHour();         // 分钟         int minute = tiem.getMinute();         // 秒         int second = tiem.getSecond();         // 纳秒         int nano = tiem.getNano();              }

3.3 时间解析

 // 解析时间     @Test     public void localTimeParse(){         // 默认支持格式解析         LocalTime parse = LocalTime.parse("22:50:00");         System.out.println(parse);// 22:50         // 指定格式解析         LocalTime time = LocalTime.parse("22:50:00", DateTimeFormatter.ISO_TIME);         System.out.println(time);// 22:50      }

3.4 时间修改

    //     @Test     public void localTime(){         // 1时间         LocalTime time = LocalTime.of(22, 50);         LocalTime time1 = time.withHour(2);//02:50         System.out.println(time1);         LocalTime time2 = time.withMinute(10);//22:10         System.out.println(time2);     }

3.5 使用Duration获得时间差值

  @Test     public void localTime(){         LocalTime time1 = LocalTime.of(22, 50,20,20);         LocalTime time2 = LocalTime.of(23, 10);         // 差值         Duration duration = Duration.between(time1, time2);         long seconds = duration.getSeconds();         int nano = duration.getNano();         System.out.println(seconds);//1179         System.out.println(nano);//999999980      }

LocalDate 和 LocalTime 能相互合并成 LocalDateTime ,LocalDateTime 也可以转为 LocalDate 或者 LocalTime。
LocalDateTime其他的API 跟 LocalTime,LocalDate 差不多,在次不赘述。

    // youku1327 谢谢 lsc      @Test     public void LocalDateTimeTest(){         LocalDate date = LocalDate.of(2019, 10, 27);         LocalTime time = LocalTime.of(23, 20, 00);         // 合并为 LocalDateTime         LocalDateTime localDateTime = LocalDateTime.of(date, time);         System.out.println(localDateTime);//2019-10-27T23:20         // 转为LocalDate         LocalDate localDate = localDateTime.toLocalDate();         System.out.println(localDate);//2019-10-27         // 转为 LocalTime         LocalTime localTime = localDateTime.toLocalTime();         System.out.println(localTime);// 23:20     }

java8中 java.time.ZoneId代替了老版本java.util.TimeZone 。

5.1 时区偏移

默认是当前时区和UTC /格林威治的固定偏差值

     @Test     public void TimeZoneId(){         // 上海         ZoneId shanghai = ZoneId.of("Asia/Shanghai");         LocalDate date = LocalDate.of(2019, 10, 27);         // 设置时区         ZonedDateTime zonedDateTime = date.atStartOfDay(shanghai);         // 获得偏移         ZoneOffset offset = zonedDateTime.getOffset();         System.out.println(offset);//+08:00     }

5.2 TimeZone 转 ZoneId

    @Test     public void TimeZoneId2(){         ZoneId zoneId = TimeZone.getDefault().toZoneId();         String id = zoneId.getId();         System.out.println(id);//Asia/Shanghai     }

5.3 时区时间计算

    @Test     public void TimeZoneId3(){         ZoneId zoneId = ZoneId.of("America/Chicago");         Instant instant = Instant.now();         // 上海时间 2019-10-27T23:51:27.168         System.out.println(LocalDateTime.ofInstant(instant,ZoneId.of("Asia/Shanghai")));         ZonedDateTime zonedDateTime = instant.atZone(zoneId);         ZoneOffset offset = zonedDateTime.getOffset();         // 美国芝加哥离上海时区差值 -05:00         System.out.println(offset);         LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();         // 芝加哥时间         System.out.println(localDateTime);//2019-10-27T10:51:27.168     }