Java(六)(LocalDate,LocalTime,LocalDateTime,ZoneId时区,Instant,DateTimeFormatter,Duration,内部类,枚举,泛型)

目录

时间(LocalDate,LocalTime,LocalDateTime)

时间(ZoneId时区)

Instant

DateTimeFormatter

Period(一段时间)

Duration(持续时间)

内部类

匿名内部类

枚举

泛型

泛型类的定义

泛型接口

泛型方法

通配符

泛型和基本数据类型


时间(LocalDate,LocalTime,LocalDateTime)

LocalDate:表示本地时间日期(年,月,日,星期)

LocalTime: 表示本地时间(时,分,秒,纳秒)

LocalDateTime: 表示本地时间(年,月,日,星期,时,分,秒,纳秒)

LocalDate的方法

public class test {
    public static void main(String[] args) {
        LocalDate ld = LocalDate.now();
        System.out.println(ld);
        //1.获取对象中的信息
        int year = ld.getYear();
        int month = ld.getMonthValue();
        int day = ld.getDayOfMonth();
        int dayOfYear = ld.getDayOfYear();
        int dayOfWeek = ld.getDayOfWeek().getValue();
        // 2. 把某个信息加多少:plusYears,plusMonths,plusDays,plusWeeks
        LocalDate d4 = ld.plusYears(2);
        LocalDate d5 = ld.plusMonths(2);

        // 3.把某个信息减多少,minusMonths,minusDays,minusWeeks
        LocalDate d6 = ld.minusYears(6);
        LocalDate d7 = ld.minusMonths(3);

        //4. 直接修改某个信息: withYear,withMonth,withOfMonth,withOfYear
        LocalDate d8 = ld.withYear(2099);
        LocalDate d9 = ld.withMonth(11);
        // 5. 获取指定日期的LocalDate对象
        LocalDate ld10 = LocalDate.of(2022,10,11);
        LocalDate ld11 = LocalDate.of(2011,11,11);
        

        }
    }

LocalTime的方法

LocalDateTime方法

其实就是将上面的两个将他们进行了合并

此外说一下独特的方法

将LocalTime对象和LocalDate合并成LocalDateTime对象

将LocalDateTime分成LocalTime和LocalDate对象

时间(ZoneId时区)

public class test {
    public static void main(String[] args) {
        // 1.ZoneId 常见方法
        // public static ZoneId systemDefault()  获取系统默认的时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId.getId());;
        System.out.println(zoneId);
        //public static Set<String> getAvailableZoneId():把某个时区id封装成ZoneId 对象
        System.out.println(ZoneId.getAvailableZoneIds());

        // public static ZoneId of(String zoneId) :把某个时区id封装成ZoneId对象
        ZoneId zoneId1 = ZoneId.of("Etc/GMT+9");
        System.out.println(zoneId1);

        // 2. ZoneDateTime : 带时区的时间
        // public static ZoneDateTime now(ZoneId zone): 获取一个时区的ZoneDateTime对象
        ZonedDateTime now = ZonedDateTime.now(zoneId1);
        System.out.println(now);

        // 世界标准时间
        ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());
        System.out.println(now1);

        // public static ZoneDateTime now()  获取系统默认时区的ZoneDateTime对象
        ZonedDateTime now2 = ZonedDateTime.now();
        System.out.println(now2);
        int month = now2.getDayOfMonth();

    }
    }

注明一点: ZonedDateTime对时间的操作和LocalDateTime差不多的

Instant

public class test {
    public static void main(String[] args) {
        // 1. 创建Instance的对象,获取此刻时间信息
        Instant now = Instant.now();

        // 2.不够1秒的纳秒数
        int nano = now.getNano();
        System.out.println(nano);

        System.out.println(now);
        Instant instant = now.plusNanos(111);

        //Instance 对象的作用,做代码的性能分析
        Instant now1 = Instant.now();
        // 代码执行
        
        Instant now2 = Instant.now();

    }
}

DateTimeFormatter

格式化器,用于时间的格式化,解析,这个类线程安全

public class test {
    public static void main(String[] args) {
        // 创建一个日期时间格式化器对象出来 // ofPattern 是static修饰的,直接  类名.方法名
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

        // 2. 对时间格式化,now也是static 修饰的 直接  类名.方法名()
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        String rs= formatter.format(now);
        System.out.println(rs);

        // 3.格式化时间  format是  对象.方法
        String rs2 = now.format(formatter);
        System.out.println(rs2);

        // 4. 解析时间 , 解析时间一般使用LocalDateTime 提供的解析方法来解析
        String dateStr = "2009年12月12日 12:12:11";
        LocalDateTime ldt = LocalDateTime.parse(dateStr,formatter);
        System.out.println(ldt);
    }
}

Period(一段时间)

计算天数,月数,年数,不能计算小时,分钟啥的

public class test {
    public static void main(String[] args) {

        LocalDate start = LocalDate.of(2022,8,12);
        LocalDate end = LocalDate.of(2029,8,23);
        // 1. 创建Period对象,封装两个日期对象
        Period period = Period.between(start,end);

        // 2.通过period对象获取两个日期对象相差的日期
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }
}

Duration(持续时间)

计算天数,月数,年数,小时数,分数,纳秒数

支持LocalTime,LocalDateTime,Instant对象

public class test {
    public static void main(String[] args) {
    LocalDateTime start = LocalDateTime.of(2022,8,12,11,11,11);
    LocalDateTime end = LocalDateTime.of(2029,8,23,12,12,12);
    Duration duration = Duration.between(start,end)

        // 2.获取两个时间对象间隔的信息  对象.方法名()就行了
       System.out.println(duration.toDays());
      System.out.println(duration.toSeconds());
        //...

    }
}
public class test {
    public static void main(String[] args) {
        // 通过duration来对程序进行计时,测试性能
        Instant st1 = Instant.now();

        int sum = 0;
        for (int i = 0; i < 1000000; i++) {
            sum += i;
        }
        Instant st2 = Instant.now();
        // 1.得到Duration对象
        Duration duration = Duration.between(st1,st2);

        System.out.println(duration.getNano());
        
    }
}

内部类

一个类定义在另一个类的内部,这个类就是内部类

下面是怎么创建内部类的对象

内部类是可以访问外部类的成员变量和方法的

匿名内部类

书写格式:

new 类或者接口(参数名....)

{

类体(一般是方法重写);

}

枚举

枚举是一种特殊的类

修饰符 enum 枚举类名{

名称1,名称2,...;

其他成员....

}  这些名称,本质是常量,每个常量都会记住枚举类的一个对象

public enum A{

X,Y,Z;

...}

(1)枚举类的第一行只能罗列一些名称,这些名称都是常量,并且每个常量记住的都是枚举类的一个对象

(2)枚举类的构造器都是私有的(写不写都是私有的),因此,枚举对外不能创建对象,所以就对应上我们上面写好这些名称,每个名称就是一个对象,不能在外面再次创建对象了

(3)枚举都是最终类,不可以被继承

(4)枚举类中可以定义其他成员

泛型

定义类,接口,方法时,同时声明一个或者多个类型变量(如:<E>),称为泛型类,泛型接口,泛型方法,他们称之为泛型

泛型类的定义

public class ArrayList<E>{

}

<>中也可以是两个数据类型

泛型接口

泛型方法

我们看上面这个图,左边是泛型方法,右边不是泛型方法,为什么呢?因为第二种的E是类来指定数据类型的,而第一种是自己来申请的泛型

通配符

? 表示通配符,可以表示任何的数据类型,也可以加上限制,图中  ? extends Car  表示只有Car或者Car子类才能进入方法种

? super Car:  ?能接收的必须是Car或者其父类

泛型和基本数据类型

泛型是不支持基本数据类型,只能支持对象类型

前面写的Integer是对象数据类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值