jdk8的datetime时间函数使用示例
发布时间:2021-07-03 16:42:37
JDK8已发布,写了一个datetime时间函数使用方法的小示例
package datetime;
import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
import static java.time.temporal.TemporalAdjusters.previousOrSame;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class DataTimes {
public static void main(String[] args) {
// 创建时间对象
LocalDateTime timePoint = LocalDateTime.now(); // 当前时间
System.out.println("--当前时间----");
System.out.println(timePoint);
System.out.println("");
System.out.println("--自定义时间----");
System.out.println(LocalDate.of(2012, Month.DECEMBER, 12)); // from
// values
System.out.println(LocalDate.ofEpochDay(150)); // middle of 1970
System.out.println(LocalTime.of(17, 18)); // the train I took home today
System.out.println(LocalTime.parse("10:15:30")); // From a String
System.out.println("");
System.out.println("--获取时间的各个部分----");
System.out.println(timePoint.toLocalDate());
System.out.println(timePoint.getMonth());
System.out.println(timePoint.getDayOfMonth());
System.out.println(timePoint.getSecond());
System.out.println("");
System.out.println("---设置并返回新的时间对象---");
LocalDateTime thePast = timePoint.withDayOfMonth(10).withYear(2010);
System.out.println(thePast);
System.out.println("---再加3周---");
LocalDateTime yetAnother = thePast.plusWeeks(3).plus(3,
ChronoUnit.WEEKS);
System.out.println(yetAnother);
System.out.println("");
System.out.println("---使用时间调整函数---");
System.out.println(timePoint);
System.out.println(timePoint.with(lastDayOfMonth()));
System.out.println(timePoint.with(previousOrSame(DayOfWeek.WEDNESDAY)));
System.out.println(timePoint.with(LocalTime.now()));
System.out.println("");
System.out.println("---截断时间精确位--");
System.out.println(timePoint);
LocalDateTime truncatedTimeToMinutes = timePoint
.truncatedTo(ChronoUnit.MINUTES);
System.out.println(truncatedTimeToMinutes);
LocalDateTime truncatedTimeToSeconds = timePoint
.truncatedTo(ChronoUnit.SECONDS);
System.out.println(truncatedTimeToSeconds);
System.out.println("");
System.out.println("---使用时区---");
ZonedDateTime zoned_now = ZonedDateTime.of(LocalDateTime.now(),
ZoneId.of("UTC+08:00"));
System.out
.println(zoned_now.withZoneSameInstant(ZoneId.of("UTC+00:00")));
System.out.println(zoned_now.getOffset());
System.out.println("");
System.out.println("---时间上使用时区偏移---");
OffsetTime time = OffsetTime.now();
ZoneOffset offset = ZoneOffset.of("+02:00");
System.out.println(time);
System.out.println(time.withOffsetSameInstant(offset));
System.out.println("");
System.out.println("---时间加减---");
timePoint = LocalDateTime.now();
System.out.println(timePoint);
// 3 years, 2 months, 1 day
Period period1 = Period.of(3, 2, 1);
System.out.println(timePoint.plus(period1));
Duration duration = Duration.ofSeconds(3, 5);
System.out.println(timePoint.plus(duration));
Duration sixHours = Duration.between(
ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+08:00")),
ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+02:00")));
System.out.println(timePoint.plus(sixHours));
System.out.println("");
}
}
运行结果
--当前时间----
2014-03-25T17:03:40.553
--自定义时间----
2012-12-12
1970-05-31
17:18
10:15:30
--获取时间的各个部分----
2014-03-25
MARCH
25
40
---设置并返回新的时间对象---
2010-03-10T17:03:40.553
---再加3周---
2010-04-21T17:03:40.553
---使用时间调整函数---
2014-03-25T17:03:40.553
2014-03-31T17:03:40.553
2014-03-19T17:03:40.553
2014-03-25T17:03:40.583
---截断时间精确位--
2014-03-25T17:03:40.553
2014-03-25T17:03
2014-03-25T17:03:40
---使用时区---
2014-03-25T09:03:40.583Z[UTC]
+08:00
---时间上使用时区偏移---
17:03:40.585+08:00
11:03:40.585+02:00
---时间加减---
2014-03-25T17:03:40.585
2017-05-26T17:03:40.585
2014-03-25T17:03:43.585000005
2014-03-25T23:03:40.586
猜你喜欢
- 前言:好久没有写博客,最近一年感觉真是好忙,各种做不完的工作。相信很多上班族都会有这种感觉。最近对NFC进行写卡操作,需要计算一个校验位。一
- const和readonly经常被用来修饰类的字段,两者有何异同呢?const1、声明const类型变量一定要赋初值吗?一定要赋初值publ
- 用一个7 x 7的矩形表示迷宫,0和1分别表示的是通路和障碍。通过设计编写程序找到蓝色小球达到蓝色旗子的路线思路:构建一个迷宫(用二维数组)
- JAVA虽然是在C++基础上发展而来,但却对C++的许多缺陷有所改进,其中一个不得不提的就是字符串,我们知道,随着学习的深入,进入MFC时,
- 参考:How to catch an Exception from a threadIs there a way to make Runna
- 一、问题背景在Intellij idea中,新建Maven项目,在魔项目中新建多个模块,发现模块间相互调用失败二、问题原因模块间无法相互引用
- 我们知道二维数组,是在一维数组的基础上进行了维度的增加。那么在实际使用的过程中,有时候我们所需要的二维数组,它们其中的维度是不同的,这就需要
- 本文需求实现了java通过方向键控制小球移动的具体过程,供大家参考,具体内容如下需求分析:第一 要画出一个小球第二 要能通过控制方向键控制它
- 今天我们来讨论如何在项目开发中优雅地使用RocketMQ。本文分为三部分,第一部分实现SpringBoot与RocketMQ的整合,第二部分
- java类的方法,我特别喜欢《java编程思想》里面的描述,这本书说java类之间的相互通信是通过消息。比如顾客类的对象调用一个eat方法,
- 题外话: 泛型与通配符是Java语法中比较难懂的两个语法,学习泛型和通配符的主要目的是能够看懂源码,实际使用的不多。1.泛型1.1泛型的用法
- 数据库表结构departmentemployee要求一现在的要求是输入 id 把 employee 表的对应员工数据查询出来,并且查询出该员
- 感谢《Android源码设计模式解析与实战》 何红辉 关爱民 著适配器模式在我们的开发中使用率极高,从代码中随处可见的Adapter就可以判
- 在java项目开发过程中,使用properties文件作为配置基本上是必不可少的,很多如系统配置信息,文件上传配置信息等等都是以这种方式进行
- 1. 概述JDK * 是利用java反射机制 生成一个实现接口的匿名类, 在调用具体方法前调用InvocationHandler来处理Cg
- 一、前言首选,双轴快排也是一种快排的优化方案,在JDK的Arrays.sort()中被主要使用。所以,掌握快排已经不能够满足我们的需求,我们
- 一、串口连接的打开与关闭串口,即COM口,在.NET中使用 SerialPort 类进行操作。串口开启与关闭,是涉及慢速硬件的IO操作,频繁
- 前言SQL注入漏洞作为WEB安全的最常见的漏洞之一,在java中随着预编译与各种ORM框架的使用,注入问题也越来越少。新手代码审计者往往对J
- 前言最近工作上遇到很多批量插入的场景,但是百度很难得到我想要的结果,而且查出来的效果不是很好~所以就自己来写一份给大家参考,希望对大家有用M
- 首先,我们需要增加用户对该脚本的执行权限,即 String cmdstring = "chmod a+x test.sh