Java:日期类2

2024-09-08 01:04
文章标签 java 日期

本文主要是介绍Java:日期类2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SimpleDateFormat日期格式化类

构造

SimpleDateFormat(String pattern);

pattern是我们自己制定的日期格式,字母不能改变,但连接符可以改变

yyyy--MM--dd--HH

时间单位字母表示

Y
M
d
H
m
s

方法

String format(Date date)将Date对象按照对应格式转成String

Date parse(String source)将符合我们构造对象时指定的日期格式的字符串转为Date对象 

LocalDate与LocalDateTime

LocalDate与LocalDateTime是两个不可变时间对象,被final修饰

LocalDate为年月日,LocalDateTime为年月日时分秒

创建

创建,通过两个静态方法创建

static LocalDate/LocalDateTime now()        以目前时间创建对象

static LocalDate/LocalDateTime of(三个int传参/六个int传参)        创建对象,设置年月日或年月日时分秒

获取 

 获取对应日期字段

int getYear() 获取年份

int getMonthValue() 获取月份,不加Value获取的是枚举类型

int getDayOfMonth() 获取日

设置 

设置对应日期,返回LocalDate或LocalDateTime对象,原对象内容不变

LocalDate/LocalDateTime withYear(int year)        设置年份

LocalDate/LocalDateTime withMonth(int month)        设置月份

LocalDate/LocalDateTime withDayOfMonth(int day)        设置日

可以链式设置 

public class LocalDateText02 {public static void main(String[] args) {//of方式创建localDate对象LocalDate localdate03 = LocalDate.of(2024,3,1);//设置LocalDate d2 = localdate03.withYear(2000);LocalDate d3 = d2.withMonth(1);LocalDate d4 = d3.withDayOfMonth(2);//链式设置LocalDate D = localdate03.withYear(2000).withMonth(1).withDayOfMonth(2);//输出System.out.println(localdate03);System.out.println(d2);System.out.println(d3);System.out.println(d4);System.out.println(D);}

日期字段偏移 

puls开头方法,向后偏移

minus开头方法,向前偏移

Period与Duration类,计算时间偏差

Period类

主要计算年月日

static Period between(LocalDate d1,LocalDate d2);计算两个日期之间的差值

 

非静态方法

getYears()获取相差的年

getMonths()获取相差的月

getDays() 获取相差的天

 

差值仅仅只是直接相减,比如2024-5-1与2020-4-1,月差值为1;

import java.time.LocalDate;
import java.time.Period;public class PeriodText {public static void main(String[] args) {LocalDate date1 = LocalDate.of(2023,8,1);LocalDate date2 = LocalDate.now();//创建period对象Period p1 = Period.between(date1,date2);//获取差值System.out.println(p1.getYears());System.out.println(p1.getMonths());System.out.println(p1.getDays());}
}

 

Duration类

主要计算时分秒,相对精确,不同于Period类的直接相减,Durtion类计算的是实际的差值

利用静态方法创建Duration对象

static Duration between(Temporal statInclusive,Temporal endInclusive)

Temporal是一个接口,LocalDate和LocalDateTime都是Temporal的实现类,但我们传参传LocalDateTime对象,因为计算精确时间偏差。

偏差获取

toDays()        获取相差天数

toHours()        获取相差小时

toMinutes()         获取相差分钟

toMillis()             获取相差秒

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;public class DurationText {public static void main(String[] args) {//创建LocalDaTeTime对象LocalDateTime date3 = LocalDateTime.of(2023,9,1,10,10,27);LocalDateTime date4 = LocalDateTime.now();//创建DuraTion对象Duration duration = Duration.between(date3,date4);//获取System.out.println(duration.toDays());System.out.println(duration.toHours());System.out.println(duration.toMinutes());System.out.println(duration.toMillis());}
}

 DateTimeFormatter日期格式化类

static DateTimeFormatter ofPattern(String patttern);指定格式

String format(TemporalAccessor temporal)        将日期对象按照制定规则转换为String

TemporalAccessor是Temporal的父接口,而LocalDate,LocalDateTime的都是Temporal的实现类,所以也是TempoaralAccessor的实现类

 

TemporalAccessor parse(CahrSequence text)        将符合规则字符串转为日期对象

如果想要将TemporalAccessor转换为我们常见的LocalDateTime日期对象,就需要用到LocalDateTime中的静态方法from

static LocalDateTime from(TemporalAccessor temporal)

public class DateTimeFormatterText {LocalDateTime date01 = LocalDateTime.now();//指定转换规则DateTimeFormatter formatter01 = DateTimeFormatter.ofPattern("YYYY-MM--dd  HH:mm:ss");//转换String s = formatter01.format(date01);TemporalAccessor temporal = formatter01.parse("2024--5--1  11:30:24");LocalDateTime date02 = LocalDateTime.from(temporal);
}

 

 

 

这篇关于Java:日期类2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1146672

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避