5、新时间API

5.1 概述

之前时间API存在问题:线程安全问题、设计混乱。

本地化日期时间 API:

  • LocalDate
  • LocalTime
  • LocalDateTime

Instant:时间戳。

ZoneId:时区。

Date、Instant、LocalDateTime的转换。

DateTimeFormatter:格式化类。

5.2 LocalDateTime类

表示本地日期时间,没有时区信息

@Test
public void test01(){
    //创建LocalDateTime对象

    //1、用系统当前时间创建一个日期对象
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println(localDateTime);
    LocalDate localDate = LocalDate.now();
    System.out.println(localDate);
    //2、指定时间创建一个日期对象
    System.out.println("====================");
    LocalDateTime localDateTime1 = LocalDateTime.of(2021, 5, 8, 14, 35, 20);
    System.out.println(localDateTime1);
}

@Test
public void test02(){
    LocalDateTime localDateTime = LocalDateTime.now();
    //withXXX 设置指定的日期内容  注意:LocalDateTime类是不可以变的。
    localDateTime = localDateTime.withYear(2022).withMonth(10).withDayOfMonth(30).withHour(18);

    //getXXX  获取指定日期内容
    System.out.println(localDateTime.getYear());
    System.out.println(localDateTime.getMonthValue());
    System.out.println(localDateTime.getDayOfMonth());
    System.out.println(localDateTime.getHour());
    System.out.println(localDateTime.getMinute());
    System.out.println(localDateTime.getSecond());
}

@Test
public void test03(){
    LocalDateTime localDateTime = LocalDateTime.now();
    //对日期中对应内容进行添加或者是减少
    //plusXXX 对指定的日期内容进行增加,  minusXXX对指定日期内容进行减少
    localDateTime = localDateTime.plusMonths(10).plusDays(30).minusYears(3);
    System.out.println(localDateTime.getYear());
    System.out.println(localDateTime.getMonthValue());
    System.out.println(localDateTime.getDayOfMonth());
    System.out.println(localDateTime.getHour());
    System.out.println(localDateTime.getMinute());
    System.out.println(localDateTime.getSecond());
}
5.3 DateTimeFormatter类

DateTimeFormatter是时间格式化类。

@Test
public void test04(){
   //将LocalDateTime转换成String类型
    //LocalDateTime localDateTime = LocalDateTime.now();
    //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
    //String time = dateTimeFormatter.format(localDateTime);
    //System.out.println(time);

    //将String类型的时间转换成LocalDateTime类型
    String time = "2021年05月08日 16:24:53";
    LocalDateTime localDateTime = 
        LocalDateTime.parse(time,DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"));
    System.out.println(localDateTime);
}
5.4 LocalDateTime和Date类型进行互相转换
@Test
public void test05(){
    //LocalDateTime和Date类型进行互相转换
    //LocalDateTime localDateTime = LocalDateTime.now();
    //Date date = Date.from(Instant.from(localDateTime.atZone(ZoneId.systemDefault())));
    //System.out.println(date);

    Date date = new Date();
    LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    System.out.println(localDateTime);
}

6、接口新特性

在JDK8环境中,接口中的方法不再是只能有抽象方法,还可以有静态方法和default方法。实现类只需要实现

它的抽象方法即可。其目的是为了解决扩展接口方法时,对实现类造成影响

举例使用:

public class TestInterface {
    @Test
    public void test01(){
        //使用接口中的静态方法(通过接口名.方法名直接调用)
        MyInterface.show();

        //使用接口中的默认方法(需要创建实现类对象,然后调用)
        MyClass myClass = new MyClass();
        myClass.print();
    }
}
class MyClass implements MyInterface{}
interface MyInterface{
    static void show(){
        System.out.println("这是接口定义的静态方法");
    }
    default void print(){
        System.out.println("这是接口定义的默认方法");
    }
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐