JDK8-日期时间新方式
日期时间新方式
在日常开发中,对于日期操作是非常常见的,但是对于有经验的开发人员来说Java8之前的日期操作是有较大问题 的。比方说SimpleDateFormat。但是在Java8之后提出了DateTimeFormatter用于解决之前的问题。
SimpleDateFormat的那些坑
SimpleDateFormat本身是线程不安全的,同时继承的DateFormat类也不是线程安全的,在多线程环境下,如果多个线程使用同一个类解析日期,如果将SimpleDateFormat定义为static,所以在多线程下它的实例会被多线程共享,线程之间相互读取时间,就会出现时间差异和其他的那些异常。
DateTimeFormatter 、LocalDate、LocalTime、LocalDateTime
Java8对于日期时间操作提供了一些新类供我们进行使用,现在可以通过DateTimeFormatter来替换掉 SimpleDateFormat。通过LocalDate、LocalTime、LocalDateTime类来操作日期+时间。而且由源码可知,这些类都是不可变更,线程安全的类。其内部提供了若干用于操作日期的方法。
DateTimeFormatter 、LocalDate、LocalTime、LocalDateTime的常用方法
/**
* @author 我是七月呀
* @date 2020/12/28
*/
public class LocalDateDemp {
public static void main(String[] args) {
/**
* 根据自己需求获取任意时间,例如:2020-12-28 14:57:00
*
*/
LocalDateTime of = LocalDateTime.of(2020, 12, 28, 14, 57, 00);
String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(format);
/**
* 注意:这里的YYYY是表示:当天所在的周属于的年份,一周从周日开始,周六结束,只要本周跨年,那么这周就算入下一年。
* 结果为:2020-12-28 15:39:37
*/
String formatOne = of.format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss"));
System.out.println(formatOne);
/**
* 获取当前时间的年月日 时分秒,例如:2020-12-28 15:39:37
*/
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = dateTimeFormatter1.format(LocalDateTime.now());
System.out.println(format1);
/**
* 获取当前时间的年月日,例如:2020-12-28
*/
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format2 = dateTimeFormatter2.format(LocalDateTime.now());
System.out.println(format2);
/**
* 获取当前时间的时分秒,例如:15:39:37
*/
DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
String format3 = dateTimeFormatter3.format(LocalDateTime.now());
System.out.println(format3);
/**
* 获取当前时间的年月日 时分秒(无符号),例如:20201228153937
*/
DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String format4 = dateTimeFormatter4.format(LocalDateTime.now());
System.out.println(format4);
/**
* 获取当前时间的年月日(无符号),例如:20201228
*/
DateTimeFormatter dateTimeFormatter5 = DateTimeFormatter.ofPattern("yyyyMMdd");
String format5 = dateTimeFormatter5.format(LocalDateTime.now());
System.out.println(format5);
/**
* 获取当前时间三天后的日期,例如:2020-12-31T15:39:37.807
* 这里的ChronoUnit.DAYS是时间单位,可以是WEEKS、MONTHS、YEARS
*/
LocalDateTime plus = LocalDateTime.now().plus(3, ChronoUnit.DAYS);
System.out.println(plus);
/**
* 获取当前时间三天前的日期,例如:2020-12-25T15:39:37.807
* 这里的ChronoUnit.DAYS是时间单位,可以是WEEKS、MONTHS、YEARS
*/
LocalDateTime plus1 = LocalDateTime.now().minus(3, ChronoUnit.DAYS);
System.out.println(plus1);
/**
* 获取指定时间当年的最后一天,例如:2020-12-31 23:59:59
*/
String format6 = dateTimeFormatter1.format(LocalDateTime.now().with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59));
System.out.println(format6);
/**
* 获取指定时间当年的第一天,例如:2020-01-01 00:00:00
*/
String format7 = dateTimeFormatter1.format(LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear()).withHour(00).withMinute(00).withSecond(00));
System.out.println(format7);
String format8 = dateTimeFormatter1.format(LocalDateTime.now().withDayOfYear(1).withHour(00).withMinute(00).withSecond(00));
System.out.println(format8);
/**
* 获取指定时间的前三天,例如:2020-12-25
*/
LocalDate minus = LocalDate.now().minus(3, ChronoUnit.DAYS);
System.out.println(minus);
LocalDate localDate = LocalDate.now().minusDays(3);
System.out.println(localDate);
LocalDateTime minus1 = LocalDateTime.now().minus(3, ChronoUnit.DAYS);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format9 = minus1.format(dateTimeFormatter);
System.out.println(format9);
/**
* 获取指定时间的后三天,例如:2020-12-31
*/
LocalDate plus2 = LocalDate.now().plus(3, ChronoUnit.DAYS);
System.out.println(plus2);
LocalDate localDate1 = LocalDate.now().plusDays(3);
System.out.println(localDate1);
LocalDateTime plus3 = LocalDateTime.now().plus(3, ChronoUnit.DAYS);
DateTimeFormatter dateTimeFormatter6 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format10 = plus3.format(dateTimeFormatter6);
System.out.println(format10);
}
}
JDK8-日期时间新方式的更多相关文章
- JDK8日期时间操作小汇总
统一使用java.time.*包下的类 1.获取当前的日期.时间.日期加时间 LocalDate todayDate = LocalDate.now(); //今天的日期 LocalTime now ...
- Java8新特性_日期时间新类 LocalDate、LocalTime、LocalDateTime
import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeForma ...
- Spring 日期时间处理
1 Spring自身的支持 1.1 factory-bean <bean id="dateFormat" class="java.text.SimpleDateFo ...
- jdk8环境下sprngboot/springmvc中JSR310新日期/时间类LocalDateTime显示效果带T
如图所示: 日期时间类中带了一个T,以上这种格式LocalDateTime格式化的时候默认日期时间格式:ISO.DATE_TIME(按笔者目前的知识理解是ISO8601规范中的日期时间格式化) 想要把 ...
- JDK8中新日期时间API
它们面临的问题是:可变性:像日期和时间这样的类应该是不可变的.偏移性:Date中的年份是从1900开始的,而月份都从0开始.格式化:格式化只对Date有用,Calendar则不行.此外,它们也不是线程 ...
- 详解 JDK8 新增的日期时间类
JDK8 新增的日期时间类 在本人之前的博文<处理时间的类 -- System类.Date类 .SimpleDateFormat类 与 Calendar类>中,讲到过表示时间的类,有三类: ...
- JDK8 新增的日期时间API
背景 JDK8中增加了一套全新的日期时间API,这里进行总结下,方便查询使用. 新的时间及日期API位于 java.time 包中,下面是一些关键类. Instant:代表的是时间戳. LocalDa ...
- 【java】JDK1.8时间日期库 新特性 所有java中时间Date的使用
除了lambda表达式,stream以及几个小的改进之外,Java 8还引入了一套全新的时间日期API,在本篇教程中我们将通过几个简单的任务示例来学习如何使用java 8的这套API.Java对日期, ...
- Java日期时间API系列7-----Jdk8中java.time包中的新的日期时间API类的特点
1.不变性 新的日期/时间API中,所有的类都是不可变的,这对多线程环境有好处. 比如:LocalDateTime 2.关注点分离 新的API将人可读的日期时间和机器时间(unix timestamp ...
随机推荐
- c++11-17 模板核心知识(十)—— 区分万能引用(universal references)和右值引用
引子 如何区分 模板参数 const disqualify universal reference auto声明 引子 T&&在代码里并不总是右值引用: void f(Widget&a ...
- 痞子衡嵌入式:一个奇怪的Keil MDK下变量链接强制对齐报错问题(--legacyalign)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是一个奇怪的Keil MDK下变量链接强制对齐报错问题. 痞子衡最近一直在参与恩智浦SBL项目(就是一个适用LPC和i.MXRT的完整OT ...
- Spring中的Mybatis
1. 前言 在构建一个web应用时基本的套路就是SSM,其中的M就是Mybatis. Mybatis作为一款开源的ORM框架, 由于其易于上手的特点成为当下比较流行的ORM框架,当然它还有一款插件能够 ...
- 在 CentOS 7 安装 Tomcat
一. 安装 JDK 8 1.1 下载 JDK 8 cd /opt/ wget --no-cookies --no-check-certificate --header "Cookie: gp ...
- NodeJS+formidable实现文件上传加自动重命名
前述 本人node初学者,此前使用原生node实现文件上传时遇到了一些困难,只做到了.txt 和.png两中格式的文件可以正常上传,如果上传其他格式文件服务端保存的文件会无法正常打开,原因是对form ...
- Python使用property函数定义的属性名与其他实例变量重名会怎么样?
首先如果定义的属性名与该属性对应的操作方法操作的实例对象同名就会触发无穷的递归调用,相关部分请参考<Python案例详解:使用property函数定义与实例变量同名的属性会怎样?> 但如果 ...
- kickstart 谷歌 D 2020 年 7 月 12 日 13: 00 - 16: 00
https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff08/0000000000386d5c (kick st ...
- es6 Object对象扩展新方法
ES6给Object拓展了许多新的方法,如: keys(obj):获取对象的所有key形成的数组 var obj = { foo: 'bar', baz: 42 }; Object.keys ...
- 重磅!Panda Global获悉立陶宛下周将发行区块链数字货币!
近日,Panda Global从路透社获悉,立陶宛将在下周开始预售2.4万枚由央行发行的数字货币.该名为LBCoin的数字货币基于区块链技术生产,也是该国试点具有国家支持背景的数字货币和区块链技术的项 ...
- CF1400G - Mercenaries
1400G - Mercenaries 考场上想到枚举,但是只想到了 \(2 ^ m\) 枚举矛盾,然后用 NOI Online 2 游戏 类似的容斥掉,结果式子推着推着就复杂度爆了 wtcl. (U ...