JDK1.8 新增的日期时间API、Stream流API
一、日期时间API
(一)获取对象的方法
方式1通过静态方法 now();
例如:LocalDateTime ldt = LocalDateTime.now();
方式2通过静态方法of()方法参数可以指定年月日时分秒
例如:LocalDateTime of = LocalDateTime.of(2018, 12, 30, 20, 20, 20);
#常用方法
1.与获取相关的方法:get系类的方法
ldt.getYear();获取年
ldt.getMinute();获取分钟
ldt.getHour();获取小时
getDayOfMonth 获得月份天数(1-31)
getDayOfYear 获得年份天数(1-366)
getDayOfWeek 获得星期几(返回一个 DayOfWeek枚举值)
getMonth 获得月份, 返回一个 Month 枚举值
getMonthValue 获得月份(1-12)
getYear 获得年份
2.格式化日期日期字符串的方法 format()
例如:String yyyy = ldt.format(DateTimeFormatter.ofPattern("yyyy"));
3.转换的方法 toLocalDate();toLocalTime();
例如:LocalDate localDate = ldt.toLocalDate();
例如:LocalTime localTime = ldt.toLocalTime();
4.判断的方法
isAfter()判断一个日期是否在指定日期之后
isBefore()判断一个日期是否在指定日期之前
isLeapYear()判断是否是闰年注意是LocalDate类中的方法
例如: boolean after = ldt.isAfter(LocalDateTime.of(2024, 1, 1, 2, 3));
例如 boolean b= LocalDate.now().isLeapYear();
5.解析的静态方法parse("2007-12-03T10:15:30");
paser() 将一个日期字符串解析成日期对象,注意字符串日期的写法的格式要正确,否则解析失败
例如:LocalDateTime parse = LocalDateTime.parse("2007-12-03T10:15:30")
按照我们指定的格式去解析:
注意细节:如果用LocalDateTime 想按照我们的自定义的格式去解析,注意
日期字符串的 年月日时分秒要写全,不然就报错
LocalDateTime ldt4 = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime.parse("2018-01-21 20:30:36", formatter2);
6.添加年月日时分秒的方法 plus系列的方法 都会返回一个新的LocalDateTime的对象
LocalDateTime localDateTime = ldt.plusYears(1);
LocalDateTime localDateTime1 = ldt.plusMonths(3);
LocalDateTime localDateTime2=ldt.plusHours(10);
7.减去年月日时分秒的方法 minus 系列的方法 注意都会返回一个新的LocalDateTime的对象
例如:LocalDateTime localDateTime2 = ldt.minusYears(8);
8.指定年月日时分秒的方法 with系列的方法 注意都会返回一个新的LocalDateTime的对象
例如 LocalDateTime localDateTime3 = ldt.withYear(1998);
//获取这个月的第几个星期几是几号,比如 TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.FRIDAY) 代表的意思是这个月的第二个星期五是几号
// TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.FRIDAY)
LocalDateTime with1 = now.with(TemporalAdjusters.dayOfWeekInMonth(2,DayOfWeek.FRIDAY));
(二) Duration : 用于计算两个“时间”间隔的类
Period : 用于计算两个“日期”间隔的类
Duration类中静态方法between()
Instant start = Instant.now();
for(int i=0;i<1000L;i++){
System.out.println("循环内容");
}
Instant end = Instant.now();
静态方法:between() 计算两个时间的间隔,默认是秒
Duration between = Durati’on.between(start, end);
Duration中的toMillis()方法:将秒转成毫秒
System.out.println(between.toMillis());
Period类 中的静态方法between()
计算两个日期之间的间隔
LocalDate s = LocalDate.of(1985, 03, 05);
LocalDate now = LocalDate.now();
Period be = Period.between(s, now);
System.out.println(be.getYears());间隔了多少年
System.out.println(be.getMonths());间隔了多少月
System.out.println(be.getDays());间隔多少天
(三)TemporalAdjuster : 时间校正器,是个接口
LocalDate now = LocalDate.now();
System.out.println(now);
1 使用TemporalAdjusters自带的常量来设置日期
LocalDate with = now.with(TemporalAdjusters.lastDayOfYear());
System.out.println(with);
2 采用TemporalAdjusters中的next方法来指定日期
LocalDate date = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(date);
例如:TemporalAdjusters.next(DayOfWeek.SUNDAY) 本周的星期天
例如:TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY) 下一周的星期一
3 采用自定义的方式来指定日期 比如指定下个工作日
LocalDateTime ldt = LocalDateTime.now();
LocalDateTime workDay = ldt.with(new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
//向下转型
LocalDateTime ld = (LocalDateTime) temporal;
//获取这周的星期几
DayOfWeek dayOfWeek = ld.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return ld.plusDays(3);//如果这天是星期五,那下个工做日就加3天
} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
return ld.plusDays(2);//如果这天是星期六,那下个工做日就加2天
} else {
//其他就加一天
return ld.plusDays(1);
}
}
});
System.out.println(workDay);
(四)DateTimeFormatter :解析和格式化日期或时间的类
1.获取对象的方式,通过静态方法ofPattern("yyyy-MM-dd");
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.now();
2.format()方法把一个日期对象的默认格式 格式化成指定的格式
String format1 = dateFormat.format(now);
System.out.println(format1);
3.格式化日期 方式2使用日期类中的format方法 传入一个日期格式化类对象
LocalDateTime now1 = LocalDateTime.now();
使用日期类中的format方法 传入一个日期格式化类对象
使用DateTimeFormatter中提供好的日期格式常量
now1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
4.使用自定义的日期格式格式化字符串
DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//自定义一个日期格式
String time = now1.format(timeFormat);
System.out.println(time);
5. 把一个日期字符串转成日期对象
使用日期类中的parse方法传入一个日期字符串,传入对应的日期格式化类
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime parse = LocalDateTime.parse(time, timeFormat);
System.out.println(parse);
二、 Stream API
(一)概述
流(Stream) 到底是什么呢?
是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列。
集合讲的是数据,流讲的是计算!
*注意:
①Stream 自己不会存储元素。
②Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream。
③Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。
(二)Stream 的操作三个步骤
1.创建 Stream
一个数据源(如:集合、数组),获取一个流
2.中间操作
一个中间操作链,对数据源的数据进行处理
3.终止操作(终端操作)
一个终止操作,执行中间操作链,并产生结果
(三)创建Stream的方式
1.Java8 中的 Collection 接口被扩展,提供了
两个获取流的方法:
default Stream stream() : 返回一个顺序流
default Stream parallelStream() : 返回一个并行流
2.Java8 中的 Arrays 的静态方法 stream() 可以获取数组流:
static Stream stream(T[] array): 返回一个流
重载形式,能够处理对应基本类型的数组:
public static IntStream stream(int[] array)
public static LongStream stream(long[] array)
public static DoubleStream stream(double[] array)
3.由值创建流,可以使用静态方法 Stream.of(), 通过显示值创建一个流。它可以接收任意数量的参数。
public static Stream of(T… values) : 返回一个流
4.由函数创建流:创建无限流可以使用静态方法 Stream.iterate()和Stream.generate(), 创建无限流。
public static Stream iterate(final T seed, finalUnaryOperator f) 迭代
public static Stream generate(Supplier s) 生成