java8-date和timeAPI

Why should we learn a java.timeAPI

  1. The original Date and Calendar classes api is more complex and difficult to understand, the application is not very flexible.
  2. Calendar is a thread safe class can cause SimpleDateFormat thread safe.
  3. java.time is JSR 310:. Date and Time API specification developed, all of its classes are thread-safe class or enumerated type
  4. java.time simple use of the API, the calculation time can be flexible, correct time.

Two LocalDate

LocalDate date, the date and time in java.time can be separated and combined.

2.1 Creating date way

 // 创建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 read 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 value read LocalDate

ChronoField is achieved TemporalField enumerate its interfaces, in addition to 2.2 reads date we can also use ChronoField reads 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 parsing 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 manipulated using date

The difference between even get a date, you can get year, month, day, and so on to determine whether non-zero.

    @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 modification date

By withAttribute modification does not change the original date, will form the basis of a new LocalDate copy of the original date.

    // 修改
    @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 modification date

TemporalAdjuster time orthotic modification time also will not change the original date, will generate new LocalDate copies, compared to withAttribute, its API is more abundant, providing a large number of static factory methods to meet our daily development needs.

    //  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);

    }

三LocalTime

If you have mastered the basic usage above LocalDate, then learning LocalTime is very simple, because the API LocalDate LocalTime and almost all the same.

3.1 Creating 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 reading time

@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 time-resolved

 // 解析时间
    @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 modify time

    //
    @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 Use Duration get the time difference

  @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

    }

Four LocalDate and LocalTime mutual merger and conversion

LocalDate each other and can be incorporated into LocalTime LocalDateTime, LocalDateTime LocalDate or LocalTime may be converted.
LocalDateTime other API with LocalTime, LocalDate similar, not repeat them at times.

    // 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
    }

Five ZoneId

java8 in java.time.ZoneId replaces the old java.util.TimeZone.

5.1 Time Zone Offset

The default value is the current time zone and a fixed offset UTC / Greenwich

    @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 Calculation Time

    @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
    }

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/zszxz/p/12066917.html