时间之间的比较与计算相差年、月、日、小时、分钟、毫秒、纳秒以及判断闰年--LocalDateTime
创始人
2024-05-24 22:43:32
0

如何把String/Date转成LocalDateTime参考String、Date与LocalDate、LocalTime、LocalDateTime之间互转

String、Date、LocalDateTime、Calendar与时间戳之间互相转化参考String、Date、LocalDateTime、Calendar与时间戳之间互相转化

比较方法介绍

isBefore(ChronoLocalDateTime other):是否在other时间之前

isAfter(ChronoLocalDateTime other):是否在other时间之后

isEqual(ChronoLocalDateTime other):是否与other时间相等

toLocalDate().isLeapYear()

时间之间计算

方法1:采用Duration

Duration between(Temporal startInclusive, Temporal endExclusive):创建对象
toDays():endInclusive-startExclusive的天数 可正可负可为零
toHours():endInclusive-startExclusive的小时数 可正可负可为零
toMinutes(): endInclusive-startExclusive的分钟数 可正可负可为零
toMillis(): endInclusive-startExclusive的毫秒数 可正可负可为零
toNanos(): endInclusive-startExclusive的纳秒数 可正可负可为零

方法2:采用Period
Period between(LocalDate startDateInclusive, LocalDate endDateExclusive):创建对象
getYears():endDateExclusive-startDateInclusive的年 可正可负可为零
getMonths(): endDateExclusive-startDateInclusive的月 可正可负可为零
getDays() :endDateExclusive-startDateInclusive的日 可正可负可为零

具体使用:

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;/*** 测试LocalDate* @author leishen*/
public class LocalDateTest {/*** LOCAL_DATE_TIME的时间格式*/private static final DateTimeFormatter LOCAL_DATE_TIME=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss ms ns");public static void main(String[] args){//系统默认时区ZoneId zoneId = ZoneId.systemDefault();//Date-->ZonedDateTimeZonedDateTime zonedDateTime = new Date().toInstant().atZone(zoneId);//当前时间LocalDateTime now = zonedDateTime.toLocalDateTime();LocalDateTime localDateTime = LocalDateTime.of(2024, 2, 9, 14, 59,59);//isBefore(ChronoLocalDateTime other):是否在other时间之前System.out.println("local.isBefore(now) = " + localDateTime.isBefore(now));//isAfter(ChronoLocalDateTime other):是否在other时间之后System.out.println("local.isAfter(now) = " + localDateTime.isAfter(now));//isEqual(ChronoLocalDateTime other):是否与other时间相等System.out.println("local.isEqual(now) = " + localDateTime.isEqual(now));//判断当前时间是否是闰年System.out.println("local.toLocalDate().isLeapYear() = " + localDateTime.toLocalDate().isLeapYear());System.out.println();//between(Temporal startInclusive, Temporal endExclusive)Duration duration = Duration.between(now,localDateTime);//toDays():endInclusive-startExclusive的天数 可正可负可为零System.out.println("duration.toDays() = " + duration.toDays());//toHours():endInclusive-startExclusive的小时数 可正可负可为零System.out.println("duration.toHours() = " + duration.toHours());//toMinutes(): endInclusive-startExclusive的分钟数 可正可负可为零System.out.println("duration.toMinutes() = " + duration.toMinutes());//toMillis(): endInclusive-startExclusive的毫秒数 可正可负可为零System.out.println("duration.toMillis() = " + duration.toMillis());//toNanos(): endInclusive-startExclusive的纳秒数 可正可负可为零System.out.println("duration.toNanos() = " + duration.toNanos());//between(LocalDate startDateInclusive, LocalDate endDateExclusive)Period period = Period.between(now.toLocalDate(), localDateTime.toLocalDate());//getYears() endDateExclusive-startDateInclusive的年 可正可负可为零System.out.println("period.getYears() = " + period.getYears());//getMonths() endDateExclusive-startDateInclusive的月 可正可负可为零System.out.println("period.getMonths() = " + period.getMonths());//getDays() endDateExclusive-startDateInclusive的日 可正可负可为零System.out.println("period.getDays() = " + period.getDays());}}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...