Java基础知识

本文详细介绍了Java8的新特性,包括DoubleStream接口、LocalData、LocalTime类和DateTimeFormatter类的使用,以及ZonedDateTime类的时间和时区处理。此外,还深入讲解了函数式编程和lambda表达式,如函数接口、方法引用和变量引用,并探讨了Stream流编程,包括创建、中间操作、终止操作和收集器的应用。
摘要由CSDN通过智能技术生成

Java8新特性和常用类学习

DoubleStream接口

1.DoubleStream接口表示元素类型是double的数据源

2.DoubleStream接口常用的方法:

(1)max.getAsDouble():获取流中数据集的最大值,getAsDouble()是将获取的数据转换为真正的double类型的数据。

(2)strem.min().getAsDouble():获取流中数据集的最小值

(3)strem.average().getAsDouble():获取流中数据的平均值

LocalData类

1.LocalData类是使用ISO日历表示年、月、日

2.LocalDate类的常用方法

(1)LocalDate.now():获取系统当前日期。

(2)LocalDate.of(int year,int month,int dayOfMonth)按指定日期创建LocalDate对象。

(3)getYear():返回日期中的年份。

(4)getMonth():返回日期中的月份。

(5)getDayOfMonth():返回月份中的日。

注:(3)(4)(5)中返回 的数据类型是int类型的数据

LocalDate date=LocalDate.now();//获取系统当前日期
System.out.print(date.getYear()+"年");
System.out.print(date.getMonthValue()+"月");
System.out.print(date.getDayOfMonth()+"日");
System.out.print(date.toString());


/**
2021年4月2日
2021-04-02
**/

LocalTime类

1.表示一天中的时间

2.常用方法

(1)LocalTime.now();获取系统当前时间

(2)LocalTime.of(int hour,int minute,int second)按指定的时间去获取一个LocalTime对象

(3)getHour():返回指定小时

(4)getMinute():返回分钟

(5)getSecond():返回秒

//用LocalTime获取系统当前时间
LocalTime time=LocalTime.now()
System.out.print(time.getHour()+"时");
System.out.print(time.getMinute()+"分");
System.out.print(time.getSecond()+"秒");
System.out.print(time.toString);

/**
11时14分24秒
11:14:24.055
**/

DateTimeFormatter类

1.用于将字符串解析为日期对象

2.常用方法

(1)static ofPattern(String pattern);按pattern字符串指定的格式创建DateTimeFormatter对象。

(2)LocalDateTime.parse(strDate,formatter);将字符串strDate解析。

//将字符串“2014-04-01:13:24:01”格式化为一个LocalDateTime类的对象,并显示年、月、日、时、分和秒
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm;ss");
LocalDateTime  date = LocalDateTime .parse("2014-04-01:13:24:01",formatter);
System.out.println(data.toString());

/**
2014-04-01T13:24:01
**/

ZonedDateTime类

1.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值