java8之后提供了新的日期和时间相关类,接口和枚举,放在了java.time包下(之前旧的日期时间类放在java.util包),使设置日期时间更加方便。java8的新日期时间类的设计,借鉴了joda-time时间库。

ISO-8601日历系统是当今世界大多数国家/地区使用的现代民用日历系统。在闰年等规则方面和格里高利历系统(阳历)相同,该系统一直沿用至今。java8之前的日期显示是使用的CST(中央标准时间)标准。有个网站可以查看各种时间标准(例如CST,UTC等):http://zh.thetimenow.com/

描述

java.time

包含最常使用的类,基于ISO-8601标准

java.time.chrono

支持非ISO日历

java.time.format

包含日期和时间的解析与格式化

java.time.temporal

包含访问日期时间的信息与调整

java.time.zone

支持时区

java8提供了3个新的完整日期时间类:LocalDte,LocalTime和LocalDateTime,位于java.time包中,LocalDate表示一个不可变的日期对象,LocalTime表示一个不可对的时间对象;LocalDateTime表示一个不可变的日期和时间,此外还可以创建部分日期时间对象,如Year对象,YearMonth对象,MonthDay对象。

这些类都有类似的方法。这三个类没有提供共有的构造方法,创建对象需要使用静态工厂方法,主要有now()和of()方法。

目录

1.获取日期时间实例

2. 日期时间的格式化和解析

3.日期时间修改

4、时间日期相关

1.获取日期时间实例

1.1 now()方法:

public static LocalDate now() {return now(Clock.systemDefaultZone());
}

LocalDte的静态工厂方法,该方法使用默认时区获得当前日期,返回LocalDte对象。

 public static LocalTime now() {return now(Clock.systemDefaultZone());}

LocalTime的静态工厂方法,该方法使用默认时区获得当前时间,返回LocalTime对象。

 public static LocalDateTime now() {return now(Clock.systemDefaultZone());}

LocalDateTime的静态工厂方法,该方法使用默认时区获得当前日期时间,返回LocalDateTime对象。

1.2  of()方法有很多重载的方法,说明如下:

public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {LocalDate date = LocalDate.of(year, month, dayOfMonth);LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);return new LocalDateTime(date, time);}

按照指定的年、月、日、时、分、秒、纳秒获得LocalDateTime实例,秒之后直接设置纳秒。当纳秒为0是,重载方法如下:

public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) {LocalDate date = LocalDate.of(year, month, dayOfMonth);LocalTime time = LocalTime.of(hour, minute, second);return new LocalDateTime(date, time);}

同理LocalDte和LocalTime也是类似的of()方法

public static LocalTime of(int hour, int minute, int second) {HOUR_OF_DAY.checkValidValue(hour);if ((minute | second) == 0) {return HOURS[hour];  // for performance}MINUTE_OF_HOUR.checkValidValue(minute);SECOND_OF_MINUTE.checkValidValue(second);return new LocalTime(hour, minute, second, 0);}

按照指定的时、分、秒获取一个LocalTime实例。

 public static LocalDate of(int year, int month, int dayOfMonth) {YEAR.checkValidValue(year);MONTH_OF_YEAR.checkValidValue(month);DAY_OF_MONTH.checkValidValue(dayOfMonth);return create(year, month, dayOfMonth);}

按照指定的年、月、日获得一个LocalDate实例,日期中的年月日必须有效,否则会抛出异常。

日期时间参数取值范围:

参数取值范围
参数 说明
year 从-999 999 999到999 999 999的年份
month 一年中的月份,从1到12
dayOfMonth 月中的天,从1到31
hour 从0到23表示的时
minute 从0到59表示的分
second 从0到59表示的秒

1.3  参考实例:

public static void main(String[] args) throws ParseException {
//        获取当前日期System.out.println("now()方法创建的当前日期:"+LocalDate.now());
//        设置日期System.out.println("of()方法设置的日期:"+LocalDate.of(2020,8,18));
//        获取当前时间(包含毫秒)System.out.println("now()方法创建的当前时间:"+LocalTime.now());
//        设置时间(时,分,秒)System.out.println("of()方法设置的时间:"+LocalTime.of(16,14,12));
//      获取当前日期和时间System.out.println("now()方法创建的当前日期时间:"+LocalDateTime.now());
//      设置日期和时间(秒之后设置的是纳秒)System.out.println("of()方法设置的日期时间:"+LocalDateTime.of(2020,2,29,12,24,35,1000));//        只显示年和月YearMonth yearMonth=YearMonth.of(2020,3);YearMonth now=YearMonth.now();
//        只显示月和天MonthDay monthday=MonthDay.of(3,7);MonthDay nowMonthDay=MonthDay.now();}

运行之后输出在控制台的结果:

2. 日期时间的格式化和解析

java提供的日期格式化类时java.time.format.DateTimeFormatter, DateTimeFormatter中本身没有提供日期格式化和日期的解析方法,这些方法还是有LocalTime,LocalDate和LocalDateTime提供的。

2.1 日期格式化

日期格式化方法是format(),这三个类都有String format(DateTimeFormatter formatter),参数是  DateTimeFormatter类型。

2.2 日期解析

日期解析方法是parse(),这三个类每一个都有两个版本的parse方法,具体如下:

 public static LocalDateTime parse(CharSequence text) {return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME);}

使用默认格式,从一个文本字符串获取LocalDateTime实例,如:2020-03-07T17:25:30.

public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) {Objects.requireNonNull(formatter, "formatter");return formatter.parse(text, LocalDateTime::from);}

使用指定格式化,从文本字符串获取LocalDateTime实例。

 public static LocalDate parse(CharSequence text) {return parse(text, DateTimeFormatter.ISO_LOCAL_DATE);}

使用默认格式,从一个文本字符串获取LocalDate实例。如:2020-03-07

 public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) {Objects.requireNonNull(formatter, "formatter");return formatter.parse(text, LocalDate::from);}

使用指定格式化,从文字符串获取LocalDate实例。

    public static LocalTime parse(CharSequence text) {return parse(text, DateTimeFormatter.ISO_LOCAL_TIME);}

使用默认格式,从一个文本字符串获取LocalTime实例。

 public static LocalTime parse(CharSequence text, DateTimeFormatter formatter) {Objects.requireNonNull(formatter, "formatter");return formatter.parse(text, LocalTime::from);}

使用指定格式化,从文本字符串获取LocalTime实例

2.3 参考实例:

package com.xiaomifeng1010.rbacboot.common.util;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;/*** @author xiaomifeng1010* @version 1.0* @date: 2020/3/7 17:36*/public class DateTimeTest {public static void main(String[] args) {
//        创建LocalDateTime对象LocalDateTime localDateTime = LocalDateTime.now();System.out.println("当前日期时间未格式化之前:"+localDateTime);//        设置格式化类DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");String afterFormat=localDateTime.format(formatter);System.out.println("当前日期时间格式化后:"+afterFormat);//        格式化字符串"2020-02-29T12:24:23"(使用默认格式),返回localDateTimeLocalDateTime localDateTime1=LocalDateTime.parse("2020-02-29T10:15:30");System.out.println("日期时间字符串解析后(默认格式):"+localDateTime1);//        以指定格式("yyyy/MM/dd HH:mm:ss")解析日期字符串"2020/03/07 17:45:40",解析后输出的格式还是LocalDateTime默认的格式LocalDateTime localDateTime2=LocalDateTime.parse("2020/03/07 17:45:40",formatter);System.out.println("一定格式解析的日期时间:"+localDateTime2);System.out.println("---------------------------------------------------------------------------");//    创建LocalDate对象LocalDate localDate = LocalDate.now();System.out.println("当前日期未格式化前:"+localDate);//        重新设置格式化类DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");String dateFormatAfter=localDate.format(formatter1);System.out.println("当前日期格式化后:"+dateFormatAfter);//        以默认格式解析日期字符串,返回LocalDate对象LocalDate localDate1=LocalDate.parse("2020-02-29");System.out.println("日期字符串解析后:"+localDate1);//        以一定格式解析日期字符串LocalDate localDate2=LocalDate.parse("2020/03/07",formatter1);System.out.println("一定格式解析的日期:"+localDate2);System.out.println("-----------------------------------------------------------------------");//        创建LocalTimeLocalTime localTime = LocalTime.now();System.out.println("当前时间未格式前:"+localTime);//        重新设置格式化类DateTimeFormatter formatter2=DateTimeFormatter.ofPattern("HH:mm:ss");String timeFormatAfter= localTime.format(formatter2);System.out.println("当前时间格式化后:"+timeFormatAfter);//        以默认格式解析时间字符串LocalTime localTime1=LocalTime.parse("18:18:23.089");System.out.println("时间字符串解析后:"+localTime);//        以一定格式解析时间字符串LocalTime localTime2=LocalTime.parse("18:20:23",formatter2);System.out.println("一定格式解析的时间:"+localTime2);}}

运行后,在控制台中输出:

格式化类DateTimeFormatter对象是通过ofPattern(String pattern)获得,其中patter是日期和时间格式。

注意事项,在parse方法中,即解析日期和时间字符串的时候,第一个参数传入的字符串的格式,一定要和pattern的格式模式匹配,比如LocalDate localDate2=LocalDate.parse("2020/03/07",formatter1);因为DateTimeFormatter中设置的格式是“yyyy/MM/dd”,即DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");所以如果LocalDate localDate2=LocalDate.parse("2020/3/7",formatter1)写成这样,也就是月份和日只写一位,是会抛出异常DateTimeParseException。

3.日期时间修改

                                                                                     日期时间相关枚举类

枚举

描述

Month

代表月份

DayOfWeek

代表星期几

ChronoField

代表时间字段

ChronoUnit

代表字段的单位

 /*** 测试修改日期时间*/@Testpublic void modifyDateAndTimeTest(){LocalDate localDate=LocalDate.of(2020,Month.MARCH,7);LocalTime localTime = LocalTime.of(20,50,23);
//        LocalDateTime localDateTime = LocalDateTime.of(2020,3,7,20,50,23);LocalDateTime localDateTime = LocalDateTime.of(localDate,localTime);
//        对已经创建好的日期时间localDateTime进行修改,可以链式调用对年月日时分秒进行修改LocalDateTime modifyDatetime=localDateTime.withYear(2020).withMonth(5).withDayOfMonth(5).withHour(21).withMinute(10);System.out.println("修改后的日期时间localDateTime:"+modifyDatetime);
//        将日期时间设置到下一个星期三(2020年5月5号是星期二,下个星期三,就是2020年5月6号),
//        不一定是下周,上边的日期是5月6日,则下个星期三,就在下周了modifyDatetime=modifyDatetime.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));System.out.println("下一个星期三日期时间:"+modifyDatetime);
//        在已经创建好的日期localDate(日期)基础上添加时分秒(time),形成LocalDateTime类型。LocalDateTime localDateTime1=localDate.atTime(localTime);System.out.println("由localDate基础上添加time,转换成LocalDateTime类型:"+localDateTime1);
//      或者在已经创建好的日期localtime(时间)基础上添加日期,形成LocalDateTime类型。LocalDateTime localDateTime2=localTime.atDate(localDate);
//        又或者将LocalDateTime转换成localDate类型LocalDate localDate1=localDateTime2.toLocalDate();System.out.println("由LocalDateTime转换为LocalDate类型:"+localDate1);
//        在原有日期时间基础上增加日期(年月日)或时间(时分秒)LocalDateTime localDateTime3= localDateTime.plusYears(0).plusYears(1).plusDays(7).plusHours(12);System.out.println("增加年月日和小时后的日期时间:"+localDateTime3);
//        在原有日期时间基础上减少一个月localDateTime3=localDateTime3.minusMonths(1);
//        再减少4天,ChronoUnit表示时间单位,是一个枚举类localDateTime3=localDateTime3.minus(4, ChronoUnit.DAYS);System.out.println("减少一个月4天后的月份:"+localDateTime3.getMonthValue());
//        使用枚举类来设定获取日期(如获取月份中某一天)System.out.println("减少一个月4天是几号:"+localDateTime3.get(ChronoField.DAY_OF_MONTH));}

控制台输出:

4、时间日期相关 

Instant类:instant表示瞬间,顾名思义,表示的是瞬时的时间点,时间线上的某个时点

Duration:时间点之间的持续时间,间隔时间

Period:ISO-8601日历系统中基于日期的时间量,表示的区间间隔显示更易读,更人性化(显示例如2年3个月4天),间隔可能会出现负值,此类不可变并且是线程安全的,

特点:适合机器处理,存储的是秒和纳秒

 /*** 测试时间点及时间区间间隔*/@Testpublic void instantAndDurationTest(){
//        获取当前时刻Instant nowInstant=Instant.now(Clock.systemDefaultZone());
//        获取当前时刻的秒数(时间戳)long currentSeconds=nowInstant.getEpochSecond();
//        显示UTC(Coordinated Universal Time- 世界协调时间,格林威治时间)时间,对UTC时间最后加一个大写字母Z,
//        系统时间,我国在东八区,所以输出的时间距离现在相差八个小时System.out.println("即刻时间:"+nowInstant);Instant instant=Instant.ofEpochSecond(60);Duration duration1 = Duration.ofDays(3);Duration duration2=Duration.ofMinutes(20);
//        传入两个日期,比较间隔(注意比较的两个对象类型要一致,不能一个参数类型Localdate,一个参数类型LocalTime)Duration duration3=Duration.between(LocalTime.of(12,5,1),LocalTime.of(22,5,5));
//        显示方式遵循iso-860标准,表示某一时间间隔,前面加一大写字母P,但时间段后都要加上相应的代表时间的大写字母(T表示time)System.out.println(duration1);System.out.println(duration2);System.out.println(duration3);//        两个时间相隔(可以使用ChronoUnit设置具体间隔年,月,日,或者时分秒的情况)long intervalDays=ChronoUnit.DAYS.between(LocalDate.of(2020,5,1),LocalDate.of(2020,5,5));System.out.println("相差天数:"+intervalDays);Period period1=Period.between(LocalDate.of(2020,5,1),LocalDate.of(2020,5,5));System.out.println("相差间隔:"+period1);
//        生成一个期间(1年2个月10天间隔)Period period2=Period.of(1,2,10);System.out.println("产生的期间:"+period2);
//        间隔时间减少20天(并不能只能计算真实间隔,只单纯对天数进行算数运算10-20=-10)period2=period2.minusDays(20);System.out.println("减少20天后的期间:"+period2);}

控制台输出结果:

java8新日期时间类使用相关推荐

  1. 构造一个日期类java_Java8 新日期时间类(1)

    Java.time 包的优势 使用Java8,新的日期时间API引入覆盖旧的日期时间API的以下缺点. 非线程安全 - java.util.Date不是线程安全的,因此开发者必须在使用日期处理并发性问 ...

  2. 高级JAVA开发必备技能:java8 新日期时间API((一)JSR-310:ZoneId 时区和偏移量)(JAVA 小虚竹)

    技术活,该赏 点赞再看,养成习惯 大家好,我是小虚竹.之前有粉丝私聊我,问能不能把JAVA8 新的日期时间API(JSR-310)知识点梳理出来.答案是肯定的,谁让我宠粉呢.由于内容偏多(超十万字了) ...

  3. 快速了解JDK8中的新日期时间类

    积土成山,风雨兴焉:积水成渊,蛟龙生焉:积善成德,而神明自得,圣心备焉.故不积跬步,无以至千里,不积小流无以成江海.齐骥一跃,不能十步,驽马十驾,功不在舍.面对悬崖峭壁,一百年也看不出一条裂缝来,但用 ...

  4. Java8新日期和时间API

    文章目录 1 现有的日期时间API的问题 2 Java8 新的日期和时间类 3 LocalDate.LocalTime和LocalDateTime 3.1 LocalDate 3.2 LocalTim ...

  5. Java日期时间类及计算

    1. Java中与日期相关的类 1.1 java.util包 类名 具体描述 Date Date对象算是JAVA中历史比较悠久的用于处理日期.时间相关的类了,但是随着版本的迭代演进,其中的众多方法都已 ...

  6. JDK8新特性:新日期时间API全面学习

    旧版jdk对时间和日期的设计非常差 1.设计不合理 在java.util和Java.sql 包中都有日期类,java.util.Data同时包含日期和时间 而Java.sql.Date仅仅包含日期 此 ...

  7. 复习Object类_日期时间类_System类_StringBuilder_包装类以及各类的细节

    Object类_日期时间类_System类_StringBuilder_包装类以及各类的细节 主要内容 Object类 Date类 DateFormat类 Calendar类 System类 Stri ...

  8. JavaSE10:日期时间类、java比较器、System类、Math类、大数类

    写在前面 常用类的学习结束了,现在就简单整理和通过一些小demo来巩固一下吧~ JDK8.0之前的日期时间类 1. java.lang.System类 System类提供的public static ...

  9. JAVA day13,14 API、Object类、日期时间类(long,Date,Calendar,DateFormat)、String类(字符串,可变长字符串)、正则表达式、包装类

    1.API API(Application Programming Interface),应⽤程序编程接⼝.Java API是⼀本程序员的"字 典",是JDK提供给我们使⽤的类的说 ...

最新文章

  1. Django框架之跨站请求伪造
  2. Spring MVC @ModelAttribute 详解
  3. mac osx wine 1.7.5 源码编译方法及中文乱码的解决
  4. linux用while循环输出1到10,Linux Shell系列教程之(十一)Shell while循环 | Linux大学...
  5. You can't specify target table 'TS_AUTH_ADMIN' for update in FROM clause记录
  6. python *args **kargs
  7. 【Golang 快速入门】高级语法:反射 + 并发
  8. 如何选择普通索引和唯一索引?|CSDN博文精选
  9. 三星电子通信撤出中国!
  10. 【No.4 Ionic】修改 cordova 插件
  11. linux之菜鸟学习第一天
  12. 第二次冲刺站立会议10
  13. mysql 跳过授权表_跳过授权表登录后使用replace into创建root权限用户
  14. 基于java springboot仓库管理微信小程序源码
  15. 【信息安全技术】RSA算法的研究及不同优化策略的比较
  16. Windows句柄数限制
  17. CSS的小特效之:融合效果
  18. android6.0的root工具,安卓6.0怎么root?安卓6.0 root教程!
  19. 如何使用Synplify综合vivado带IP核的工程
  20. 2021浏阳一中高考成绩查询,浏阳市2021年“三考”增加考生万余名

热门文章

  1. 什么是clearfix?
  2. 如何还原已经推送到远程分支的合并提交?
  3. 在Ruby on Rails中对nil v。空v。空白的简要解释
  4. 您如何构造适合于numpy排序的数组?
  5. 统计.html,分类统计.html
  6. clion环境搭建c++_mingw64_clion中使用python,ros-qt
  7. ❤️《微服务开发—Swagger》(建议收藏)
  8. 如何进行动态的SQL环境搭建?
  9. vue项目H5 跳转微信小程序
  10. vue族谱架构_【Vue】谈Vue的依赖追踪系统 ——搞懂methods watch和compute的区别和联系...