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++stl简单使用
1 //1.sort函数排序 2 /* 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; ...
- H3CNE(教程)
培训机构提供的ppt,可能也是来自于官方提供,涉及到H3CNE认证考试中的全部知识点,学真技术还得看这个.包括帧中继,哪怕是淘汰了十多年了. https://huxiaoyao.lanzous.com ...
- 基于HAL库的STM32的DSP库详解(附FFT应用)
1 . 建立工程,生成代码时选择包含所有库. 2. 打开 option for target 选择 Target 标签,在code generatio中,将floating point hardw ...
- python基本输入输出函数
python程序设计中有三个重要的基本输入.输出函数,用于输入.转换和输出,分别是input(),eval(),print() 1,input()函数 """ input ...
- 深度优先遍历&广度优先遍历
二叉树的前序遍历,中序遍历,后序遍历 树的遍历: 先根遍历--访问根结点,按照从左至右顺序先根遍历根结点的每一颗子树. 后根遍历--按照从左至右顺序后根遍历根结点的每一颗子树,访问根结点. 先根:AB ...
- Django-DRF框架
一.RESTfull设计风格 1.什么是RESTfull? 1)REST:即Representational State Transfer的缩写.维基百科称其为"具象状态传输",国 ...
- PyQt学习随笔:Model/View中诸如DisplayRole的数据角色及含义
在PyQt中,模型可以针对不同的组件(或者组件的不同部分,比如存储数据.界面展示数据.按钮的提示等)提供不同的数据.例如,Qt.DisplayRole用于视图的文本显示.通常来说,模型中的数据项包含一 ...
- PyQt(Python+Qt)学习随笔:Qt Designer中QAbstractButton派生按钮部件的shortcut 属性
shortcut 属性保存与按钮关联的快捷键.可以使用shortcut()和setShortcut(QKeySequence)访问和设置该属性. 关于这个属性官网介绍的不多,经老猿实际验证,它与tex ...
- 《深入理解计算机系统》实验一 —Data Lab
本文是CSAPP第二章的配套实验,通过使用有限的运算符来实现正数,负数,浮点数的位级表示.通过完成这13个函数,可以使我们更好的理解计算机中数据的编码方式. 准备工作 首先去官网Lab Assig ...
- tensorflow GPU的使用
参考:https://blog.csdn.net/mzpmzk/article/details/78647711 import os # 默认情况,TF 会占用所有 GPU 的所有内存, 我们可以指定 ...