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.