• Java8中的 LocalDateTime


    获取当天初始时间

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/**
    2. * 获取传入时间的凌晨 00:00:00
    3. */
    4. public static LocalDateTime getDayStart(LocalDateTime paramTime) {
    5. if (null == paramTime) {
    6. return LocalDateTime.MIN;
    7. } else {
    8. return LocalDateTime.of(paramTime.toLocalDate(), LocalTime.MIN);
    9. }
    10. }

    获取当天结束时间

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/**
    2. * 获取传入时间当天最大的时间 23:59:59
    3. */
    4. public static LocalDateTime getDayEnd(LocalDateTime paramTime) {
    5. if (null == paramTime) {
    6. return LocalDateTime.MAX;
    7. } else {
    8. return LocalDateTime.of(paramTime.toLocalDate(), LocalTime.MAX);
    9. }
    10. }

    获取时间差

    有的时候,根据业务需求,我们会获取例如上个月的时间,或者推迟几分钟、几小时的时间等等,在 java 8之后,官方编写了现成的时间工具可以使用。下面,让我们一起来体验一下吧。

    1. class="prettyprint hljs vbscript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
    2. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    3. System.out.println("当前时间: " + fmt.format(LocalDateTime.now()));
    4. LocalDateTime time = LocalDateTime.now().minusMinutes(1L);
    5. System.out.println("获取上一分钟: " + fmt.format(time));
    6. LocalDateTime nextMin = LocalDateTime.now().plusMinutes(1L);
    7. System.out.println("获取下一分钟: " + fmt.format(nextMin));
    8. }

    输出:

    1. <pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">当前时间: 2022-08-01 09:11:16
    2. 获取上一分钟: 2022-08-01 09:10:16
    3. 获取下一分钟: 2022-08-01 09:12:16

    仔细看上述代码,其中最重要的就是 minusMinutes , plusMinutes ,字面英文大意不难看出这是什么意思,同理可得,我们去看下工具类中,还有哪些方法。

    在 plusMinutes 上右击选择转到,然后选择 实现 或者 声明或用例

    然后就会进入到 LocalDateTime.java 类中,Mac 按快捷键 command + 7 ,Windows 按快捷键 Alt + 7 ,会显示主体结构。

    我们在下图中,可以观测到官方提供了很多现有的方法,基本满足了我们日常的使用。

    常用方法简介

    方法解释
    minusDays减去指定的天数
    minusHours减去指定的小时数
    minusMinutes减去指定的分钟数
    minusSeconds减去指定的秒数
    minusNanos减去指定的纳秒数
    plusDays增加指定的天数
    plusHours增加指定的小时数
    plusMinutes增加指定的分钟数
    plusSeconds增加指定的秒数
    plusNanos增加指定的纳秒数

    上面的大多都很好理解,就其中的 minusNanos 减去指定的纳秒数,我们来实践一下:

    先来看一下,我们先输出一下当前时间的毫秒:

    1 毫秒 = 100万纳秒

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
    2. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    3. LocalDateTime current = LocalDateTime.now();
    4. System.out.println("当前时间: " + fmt.format(current));
    5. LocalDateTime tempTime = current.minusNanos(100 * 1000000L);
    6. System.out.println("减去 100 毫秒: " + fmt.format(tempTime));
    7. }

    输出:

    1. <pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">当前时间: 2022-08-01 10:00:27.800
    2. 减去 100 毫秒: 2022-08-01 10:00:27.700

    plusNanos 与其他相关的方法就不演示了,相信举一反三,同理可得。

    获取自定义时间

    现业务需要,需要获取当前小时开始的时间,例如 2022-10-01 10:00:00

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
    2. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    3. LocalDateTime current = LocalDateTime.now();
    4. System.out.println("当前时间: " + fmt.format(current));
    5. LocalDateTime tempTime = current.minusNanos(100 * 1000000L);
    6. tempTime = tempTime.withMinute(0).withSecond(0).withNano(0);
    7. System.out.println("获取当前时间,除了日期与小时,其余为 0: " + fmt.format(tempTime));
    8. }

    输出:

    1. <pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">当前时间: 2022-08-01 10:04:06.004
    2. 获取当前时间,除了日期与小时,其余为 0: 2022-08-01 10:00:00.000

    最主要的代码是:

    class="hljs ini" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">tempTime = tempTime.withMinute(0).withSecond(0).withNano(0);
    

    tips: tempTime操作完是需要返回值接收的,不然不会改变原有的值。

    可以看到用到了三个 with:

    方法解释
    withMinute设置指定的分钟数
    withSecond设置指定的秒数
    withNano设置指定的纳秒数

    这个时候有人就要玩烧的了,我就要给分钟设置个 61...,我们来试一下,报错如下:

    <pre class="prettyprint hljs xquery" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">...DateTimeException: Invalid value for MinuteOfHour (valid values 0 - 59): 61
    

    比较时间前与后

    现业务需要我们判断传入参数不能大于当前时间,我们如何去做?

    通过如下方法加示例搞定:

    方法解释
    isAfter检查此日期时间是否在指定的日期时间之后
    isBefore检查此日期时间是否在指定的日期时间之前
    1. class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    2. LocalDateTime d1 = LocalDateTime.of(2022, 8, 2, 10, 0, 0);
    3. System.out.printf("d1: %s%n", fmt.format(d1));
    4. LocalDateTime d2 = LocalDateTime.of(2022, 8, 1, 10, 0, 0);
    5. System.out.printf("d2: %s%n", fmt.format(d2));
    6. System.out.println("d1.isAfter(d2): " + d1.isAfter(d2));
    7. System.out.println("d1.isBefore(d2): " + d1.isBefore(d2));

    输出:

    1. class="prettyprint hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">d1: 2022-08-02 10:00:00.000
    2. d2: 2022-08-01 10:00:00.000
    3. d1.isAfter(d2): true
    4. d1.isBefore(d2): false
  • 相关阅读:
    Redisson实现分布式锁
    python数据分析-房价数据集聚类分析
    ArtFlow: Unbiased Image Style Transfer via Reversible Neural Flows
    php危险函数及rce漏洞
    仿制药的未来商机--个人研发的体会
    开利网络携手南京同仁堂开启“链企来”大健康企业专场沙龙会
    NOIP2011-2018提高组解题报告
    计算机基础知识
    SpringCloud之负载均衡Ribbon
    SpringCloud中Gateway提示OPTIONS请求跨域问题
  • 原文地址:https://blog.csdn.net/weixin_62421895/article/details/126136132