在日常的学习和工作中经常会和日期打交道,今日找了个时间算是系统的学习了一下Java8的新日期API。此文档包含的内容有:
- 人熟悉和理解的日期和时间格式——LocalDate 和 LocalTime
- 机器熟悉和理解的日期和时间格式——(时间戳)
- 如何计算两个日期(时间)之间的间隔
- 如何解析,修改已经创建的日期(时间)
- 编写业务代码时常遇到的几个场景
1. 人熟悉和理解的日期和时间格式——LocalDate & LocalTime
LocalDate类里包含了简单的日期,并不含有当天的时间信息,它也不含有任何的时区信息。
1.1 创建LocalDate
//创建实例 of工厂类
LocalDate nowDate = LocalDate.of(2023, 5, 15);
//创建实例 使用系统时钟获取当前时间
LocalDate localDate = LocalDate.now();
1.2 读取其中的值
其中TemporalField是一个接口,接口中定义了如何访问temporal对象某个字段的值。
ChronoField实现了TemporalField接口,可以很方便的获得枚举元素的值。
int year = nowDate.get(ChronoField.YEAR);
int yearOfMonth = nowDate.get(ChronoField.MONTH_OF_YEAR);
int dayOfMonth = nowDate.get(ChronoField.DAY_OF_MONTH);
int year1 = nowDate.getYear();
int dayOfMonth1 = nowDate.getDayOfMonth();
int month1 = nowDate.getMonthValue();
2. 机器熟悉和理解的日期和时间格式——时间戳
从计算机的角度上看,建模时间的最好的格式是从一个时间开始到建模的时间结束用一个整型数来表示这个时间的长短。通常机器以Unix元年时间(设定为UTC时区1970年1月1日午夜时分)开始所经历的秒数进行计算。
通常向静态方法:ofEpochSecond传递代表秒数的值创建一个实例:
eg:Instant也可以使用工厂方法now帮助我们创建一个当前时间的时间戳:
long epochSecond = Instant.now().getEpochSecond();
3. 如何计算两个日期(时间)之间的间隔——Duration & Period
Duration和Period可以用来衡量两个日期或时间之间相差了多久。
Duration主要是用秒和纳秒衡量时间的长短,所以不能向Between方法中传入LocalDate类型
LocalTime timeAgo = LocalTime.of(1, 10, 15);
LocalTime timeNow = LocalTime.of(1, 15, 15);
Duration bt = Duration.between(timeAgo, timeNow);
long seconds = bt.getSeconds();
System.out.println(seconds);
Period可以获取两个日期年 月 日之间的间隔时间
LocalDate ago = LocalDate.of(2023, 5, 15);
LocalDate today = LocalDate.of(2023, 5, 16);
Period period = Period.between(ago, today);
int years = period.getYears();
int months = period.getMonths();
System.out.println(years);
4.修改解析格式化日期—TimporalAdjuster & DateTimeFormatter
如果现在已经有了一个LocalDate对象,我们需要创建一个LocalDate的修改版我们有以下几种方式:
-
withAttribute方法:
twenty = twenty.withYear(2000); twenty = twenty.withMonth(12); twenty = twenty.withDayOfMonth(15); twenty = twenty.with(ChronoField.MONTH_OF_YEAR,10); twenty = twenty.with(ChronoField.YEAR,2001); //2001-10-15 -
plus || minus 方法
LocalDate twenty = LocalDate.of(2022, 1, 10); twenty=twenty.minus(1,ChronoUnit.YEARS); twenty=twenty.plus(1, ChronoUnit.MONTHS); twenty=twenty.plus(1, ChronoUnit.DAYS); System.out.println(twenty.toString()); //2021-2-11
4.1 解析日期-时间对象
java.time.format包是为了处理日期,时间对象时格式化日期,时间对象这一功能而设计的。
举个例子:我们可以把年月日的格式时间(2022-5-15)变成(20220515)或(2022-05-15)这种类型的String字符串。
DateTimeFormatter实例是线程安全的,DateTimeFormatter可以按照某个特定的模式创建格式器。
String pattern1="yyyy-MM-dd";
String pattern2="MM/dd/yyyy";
//24h
String pattern3="yyyy-MM-dd HH:mm:ss";
//12h
String pattern4="yyyy-MM-dd hh:mm:ss a";
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern(pattern1);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(pattern2);
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern(pattern3);
DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern(pattern4);
LocalDateTime time = LocalDateTime.now();
String format1 = time.format(formatter1); //2023-05-15
String format2 = time.format(formatter2); //05/15/2023
String format3 = time.format(formatter3); //2023-05-15 16:20:14
String format4 = time.format(formatter4); //2023-05-15 04:20:14 下午
5.编写业务代码时常遇到的场景
场景1:需要获取当前时间的时间戳
Timestamp currentTime = Timestamp.valueOf(LocalDateTime.now());
场景2:需要从时间戳中获取年、月、日
import java.sql.Timestamp;
import java.time.LocalDateTime;
//时间戳:2023-10-24 21:04:27.683
Timestamp currentTime = Timestamp.valueOf(LocalDateTime.now());
//时间戳转为日期时间:2023-10-24T21:04:27.683
LocalDateTime thisCurrentTime = currentTime.toLocalDateTime();
//年 2023
int year = thisCurrentTime.getYear();
//月 10
int monthValue = thisCurrentTime.getMonthValue();、
//日 24
int dayOfMonth = thisCurrentTime.getDayOfMonth();