一.背景
作为一个写业务的Java程序员,往往需要处理和日期相关的代码,比如获取当天最早和最晚时间,比如获取前三天时间,等等
- 之前都是用Date类,往往需要写N多行代码来进行转换,还需要使用SimpleDateFormat等来进行格式化,可读性非常差,Java 8提供了一组新的类方便处理时间。
java.time.LocalDate ->只对年月日做出处理
java.time.LocalTime ->只对时分秒纳秒做出处理
java.time.LocalDateTime ->同时可以处理年月日和时分秒
二.使用
基本使用方法:
// 获取今天的日期
LocalDate today = LocalDate.now();
// 今天是几号
int dayofMonth = today.getDayOfMonth();
// 今天是周几(返回的是个枚举类型,需要再getValue())
int dayofWeek = today.getDayOfWeek().getValue();
// 今年是哪一年
int dayofYear = today.getDayOfYear();
来一些复杂的:
// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2);
// 取本月最后一天,不需要人为计算本月有几天
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
// 取下一天:
LocalDate firstDayOfNextMonth = lastDayOfThisMonth.plusDays(1);
三.类似Stream的函数式编程形式
Java 8新增的Stream借鉴了函数式编程的思路,让代码可读性更好,下面举例说明:
需求是:获得前三天凌晨0点0分0秒的时间戳
long ts = LocalDate.now().minusDays(3)
.atStartOfDay(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
解释:
- Instant——代表的是时间戳,注意这里默认的Instant是0时区,比北京少8个时区,例子:2018-10-08T09:50:21.852Z,相当于当天北京时间的17:50:21.852
- ZoneId.systemDefault(); //这是为了获取本地的时区
- toEpochMilli()//转化为时间戳,单位为ms
四.使用总结
//获取当前日期
LocalDate now = LocalDate.now();
System.out.println("系统当前日期为:" + now);
//获取当前日期时间
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("系统当前日期时间为:" + localDateTime);
//默认时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("默认时区:" + zoneId);
//使用默认时区获取当前时间
LocalDate now1 = LocalDate.now(ZoneId.systemDefault());
System.out.println("系统当前时间(使用默认时区)为:" + now1);
//获取指定日期
LocalDate specifiedDate = LocalDate.of(2017, 9, 22);
System.out.println("指定日期为:" + specifiedDate);
//获取指定时间
LocalDateTime specifiedDateTime = LocalDateTime.of(2017, 9, 22, 15, 40, 01);
System.out.println("指定时间为:" + specifiedDateTime);
//获取相差的天数 单位:ChronoUnit.DAYS 天数
LocalDate yesterday = LocalDate.of(2018, 1, 1);
LocalDate today = LocalDate.now(ZoneId.systemDefault());
long betweenDays = yesterday.until(today, ChronoUnit.DAYS);
System.out.println("相差的天数:" + betweenDays);
//获取相差的星期数
long betweenWeeks = yesterday.until(today, ChronoUnit.WEEKS);
System.out.println("相差的星期数:" + betweenWeeks);
//获取相差的世纪
LocalDate pass = LocalDate.of(1911, 1, 1);
long betweenCenturies = pass.until(today, ChronoUnit.CENTURIES);
System.out.println("相差的世纪数:" + betweenCenturies);
//获取相差的几十年 10年为1个单位
LocalDate passDecades = LocalDate.of(2000, 1, 1);
long betweenDecades = passDecades.until(today, ChronoUnit.DECADES);
System.out.println("相差几十年:" + betweenDecades);
//获取相差的纪元
LocalDate passEras = LocalDate.of(2000, 1, 1);
long betweenEras = passEras.until(today, ChronoUnit.ERAS);
System.out.println("相差的纪元:" + betweenEras);
//日期字符串解析为LocalDate对象
LocalDate parseDate = LocalDate.parse("2016-12-31");
System.out.println("字符串解析为LocalDate对象:" + parseDate);
//调整月份
LocalDate currentLocalDate = LocalDate.now();
LocalDate newMonth = currentLocalDate.withMonth(10);
System.out.print("当前的日期为:" + LocalDate.now());
System.out.println("调整当前的日期为:" + newMonth);
//调整当前的时间 不会更改本地计算机的时间
LocalDateTime newCurrentDate = currentLocalDate.atTime(18,7,9);//18:07:09 LocalDateTime没有atTime方法
System.out.print("当前的时间为:" + LocalDateTime.now());
System.out.println("调整当前的时间为:" + newCurrentDate);
//获取当天的起始时间
LocalDate localDate = LocalDate.now();
LocalDateTime startOfDay = localDate.atStartOfDay();
System.out.println("当天的起始时间为:" + startOfDay);
//获取年、月、日
LocalDate nowLocalDate = LocalDate.now();
System.out.println("年份:" + nowLocalDate.getYear());
System.out.println("英文 月份:" + nowLocalDate.getMonth());
System.out.println("数字 月份:" + nowLocalDate.getMonthValue());
System.out.println("日份:" + nowLocalDate.getDayOfMonth());
System.out.println("英文 星期:" + nowLocalDate.getDayOfWeek());
System.out.println("本年已过多少天:" + nowLocalDate.getDayOfYear());
//调整时间
LocalDateTime nowDateTime = LocalDateTime.now();
//明天
LocalDateTime plusDays = nowDateTime.plusDays(1);
System.out.println("明天此时:" + plusDays);
//昨天
LocalDateTime plusDays2 = nowDateTime.plusDays(-1);
System.out.println("昨天此时:" + plusDays2);
//获取日期 时间详细信息
System.out.println(nowDateTime.atZone(ZoneId.systemDefault()));