Java—— 常见API介绍 第五期

JDK8以后新增的时间相关类

Date类ZoneId:时区
Instant:时间戳
ZoneDateTime:带时区的时间

日期格式化类

SimpleDateFormat

DateTimeFormatter:用于时间的格式化和解析

日历类

Calendar

LocalDate:年、月、日
LocalTime:时、分、秒
LocalDateTime:年、月、日、时、分、秒
工具类Period:时间间隔(年,月,日)
Duration:时间间隔(秒,纳秒)
ChronoUnit:时间间隔(所有单位)

优势

代码层面:代码更加简单

安全层面:规定时间日期对象不可变,修改后的值记录在一个新的变量中,解决了多线程环境下数据安全问题

Zoneld时区

常见方法:

方法名说明
static Set<String> getAvailableZoneIds ()获取Java中支持的所有时区
static ZoneId systemDefault ()获取系统默认时区
static ZoneId of (String zoneId)获取一个指定时区 

代码演示:

import java.time.ZoneId;
import java.util.Set;public class Test6 {public static void main(String[] args) {//获取Java中支持的所有时区Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();//Java中支持600个时区System.out.println(availableZoneIds.size());//600//获取系统默认时区ZoneId zi1 = ZoneId.systemDefault();System.out.println(zi1);//Asia/Shanghai//获取一个指定时区ZoneId zi2 = ZoneId.of("Asia/Taipei");System.out.println(zi2);//Asia/Taipei}
}

Instant时间戳

常见方法:

方法名说明
static Instant now ()

获取当前时间的Instant对象

(标准时间)

static Instant ofXxxx (long epochMilli)

根据(秒/毫秒/纳秒)获取Instant对象

ZonedDateTime atZone (ZoneId zone)指定时区
boolean isXxx (InstantotherInstant)判断时间系列的方法
Instant minusXxx (long millisToSubtract)减少时间系列的方法
Instant plusXxx (long millisToSubtract)增加时间系列的方法

代码演示:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class Test7 {public static void main(String[] args) {//获取当前时间的Instant对象Instant it1 = Instant.now();System.out.println(it1);//2025-04-25T08:57:35.873433800Z//根据(秒/毫秒/纳秒)获取Instant对象//秒Instant it2 = Instant.ofEpochSecond(1L);System.out.println(it2);//1970-01-01T00:00:01Z//毫秒Instant it3 = Instant.ofEpochMilli(1000L);System.out.println(it3);//1970-01-01T00:00:01Z//纳秒Instant it4 = Instant.ofEpochSecond(1, 1000000000);System.out.println(it4);//1970-01-01T00:00:02Z//指定时区,中国在时间原点上加8小时Instant it5 = Instant.ofEpochMilli(0L);ZoneId zi = ZoneId.of("Asia/Shanghai");ZonedDateTime zdt = it5.atZone(zi);System.out.println(zdt);//1970-01-01T08:00+08:00[Asia/Shanghai]//判断时间系列的方法Instant it6 = Instant.ofEpochMilli(1000L);Instant it7 = Instant.ofEpochMilli(2000L);boolean flag1 = it6.isBefore(it7);System.out.println(flag1);//trueboolean flag2 = it6.isAfter(it7);System.out.println(flag2);//false//减少时间系列的方法Instant it8 = Instant.ofEpochMilli(3000L);System.out.println(it8);//1970-01-01T00:00:03Z//减1000毫秒Instant it9 = it8.minusMillis(1000L);System.out.println(it9);//1970-01-01T00:00:02Z//减1秒Instant it10 = it8.minusSeconds(1L);System.out.println(it10);//1970-01-01T00:00:02Z//减1000000000纳秒Instant it11 = it8.minusNanos(1000000000L);System.out.println(it11);//1970-01-01T00:00:02Z//增加时间系列的方法Instant it12 = Instant.ofEpochMilli(0L);System.out.println(it12);//1970-01-01T00:00:00Z//加1000毫秒Instant it13 = it12.plusMillis(1000L);System.out.println(it13);//1970-01-01T00:00:01Z//加1秒Instant it14 = it12.plusSeconds(1L);System.out.println(it14);//1970-01-01T00:00:01Z//加1000000000纳秒Instant it15 = it12.plusNanos(1000000000L);System.out.println(it15);//1970-01-01T00:00:01Z}
}

ZoneDateTime带时区的时间

常见方法:

方法名说明
static ZonedDateTime now ()获取当前时间的ZonedDateTime对象
static ZonedDateTime ofXxxx (......)获取指定时间的ZonedDateTime对象
ZonedDateTime withXxx (时间)修改时间系列的方法
ZonedDateTime minusXxx (时间)减少时间系列的方法
ZonedDateTime plusXxx (时间)增加时间系列的方法

代码演示:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class Test8 {public static void main(String[] args) {//获取当前时间(带时区)ZonedDateTime zdt1 = ZonedDateTime.now();System.out.println(zdt1);//2025-04-25T17:26:50.183160400+08:00[Asia/Shanghai]//获取指定时间//年、月、日、时、分、秒、纳秒方式指定ZonedDateTime zdt2 = ZonedDateTime.of(2020, 10, 10,10, 10, 10, 0,ZoneId.of("Asia/Shanghai"));System.out.println(zdt2);//2020-10-10T10:10:10+08:00[Asia/Shanghai]//Instant+时区指定Instant it = Instant.ofEpochMilli(0L);ZoneId zi = ZoneId.of("Asia/Shanghai");ZonedDateTime zdt3 = ZonedDateTime.ofInstant(it, zi);System.out.println(zdt3);//1970-01-01T08:00+08:00[Asia/Shanghai]//修改时间系列的方法//例如:修改年份为2021ZonedDateTime zdt4 = zdt3.withYear(2021);System.out.println(zdt4);//2021-01-01T08:00+08:00[Asia/Shanghai]//同理,其他字段也可以修改//减少时间系列的方法//例如:减少1小时ZonedDateTime zdt5 = zdt3.minusHours(1);System.out.println(zdt5);//1970-01-01T07:00+08:00[Asia/Shanghai]//增加时间系列的方法//例如:增加1年ZonedDateTime zdt6 = zdt3.plusYears(1);System.out.println(zdt6);//1971-01-01T08:00+08:00[Asia/Shanghai]}
}

DateTimeFormatter时间的格式化和解析

常见方法:

方法名说明
static DateTimeFormatter ofpattern (格式)获取格式对象
string format (时间对象)按照指定方式格式化

代码演示:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;public class Test9 {public static void main(String[] args) {//static DateTimeFormatter ofpattern (格式)	获取格式对象//string format (时间对象)	按照指定方式格式化//创建带时区的时间对象ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0L),ZoneId.of("Asia/Shanghai"));//获取格式对象DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//按照指定方式格式化String str = dtf.format(zdt);System.out.println(str);//1970-01-01 08:00:00}
}

LocalDate、LocalTime、LocalDateTime

常见方法:

方法说明
static xxx now ()获取当前时间的对象
static xxx of (......)获取指定时间的对象
get开头的方法

获取日历中的年、月、日、

时、分、秒等信息

isBefore, isAfter比较两个时间
with开头的方法修改时间系列方法
minus开头的方法减少时间系列方法
plus开头的方法增加时间系列方法
public LocalDate toLocalDate ()LocalDateTime转换成一个LocalDate对象
public LocalTime toLocalTime ()LocalDateTime转换成一个LocalTime对象

代码演示:

LocalDate:(年,月,日)
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;public class Test10 {public static void main(String[] args) {//1.获取当前时间的日历对象(包含 年月日)LocalDate nowDate = LocalDate.now();System.out.println("今天的日期:" + nowDate);//今天的日期:2025-04-25//2.获取指定的时间的日历对象LocalDate ldDate = LocalDate.of(2023, 1, 1);System.out.println("指定日期:" + ldDate);//指定日期:2023-01-01//3.get系列方法获取日历中的每一个属性值//获取年int year = ldDate.getYear();System.out.println("year: " + year);//year: 2023//获取月//方式一:Month m = ldDate.getMonth();System.out.println(m);//JANUARYSystem.out.println(m.getValue());//1//方式二:int month = ldDate.getMonthValue();System.out.println("month: " + month);//month: 1//获取日int day = ldDate.getDayOfMonth();System.out.println("day:" + day);//day:1//获取一年的第几天int dayofYear = ldDate.getDayOfYear();System.out.println("dayOfYear:" + dayofYear);//dayOfYear:1//获取星期DayOfWeek dayOfWeek = ldDate.getDayOfWeek();System.out.println(dayOfWeek);//SUNDAYSystem.out.println(dayOfWeek.getValue());//7//is开头的方法表示判断System.out.println(ldDate.isBefore(ldDate));//falseSystem.out.println(ldDate.isAfter(ldDate));//false//with开头的方法表示修改,只能修改年月日LocalDate withLocalDate = ldDate.withYear(2000);System.out.println(withLocalDate);//2000-01-01//minus开头的方法表示减少,只能减少年月日LocalDate minusLocalDate = ldDate.minusYears(1);System.out.println(minusLocalDate);//2022-01-01//plus开头的方法表示增加,只能增加年月日LocalDate plusLocalDate = ldDate.plusDays(1);System.out.println(plusLocalDate);//2023-01-02}
}
LocalTime:(时、分、秒)
import java.time.LocalTime;public class Test11 {public static void main(String[] args) {// 获取本地时间的日历对象。(包含 时分秒)LocalTime nowTime = LocalTime.now();System.out.println("今天的时间:" + nowTime);//今天的时间:18:34:24.363704100int hour = nowTime.getHour();//时System.out.println("hour: " + hour);//hour: 18int minute = nowTime.getMinute();//分System.out.println("minute: " + minute);//minute: 34int second = nowTime.getSecond();//秒System.out.println("second:" + second);//second:24int nano = nowTime.getNano();//纳秒System.out.println("nano:" + nano);//nano:363704100//指定时分System.out.println(LocalTime.of(8, 20));//08:20//指定时分秒System.out.println(LocalTime.of(8, 20, 30));//08:20:30//时分秒纳秒LocalTime mTime = LocalTime.of(8, 20, 30, 150);System.out.println(mTime);//08:20:30.000000150//is系列的方法System.out.println(nowTime.isBefore(mTime));//falseSystem.out.println(nowTime.isAfter(mTime));//true//with系列的方法,只能修改时、分、秒System.out.println(mTime.withHour(10));//10:20:30.000000150//plus系列的方法,只能修改时、分、秒System.out.println(mTime.plusHours(10));//18:20:30.000000150}
}
LocalDateTime:(年、月、日、时、分、秒)
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;public class Test12 {public static void main(String[] args) {// 当前时间的的日历对象(包含年月日时分秒)LocalDateTime nowDateTime = LocalDateTime.now();System.out.println("今天是:" + nowDateTime);//今天是:2025-04-25T18:40:32.776723800//年System.out.println(nowDateTime.getYear());//2025//月System.out.println(nowDateTime.getMonthValue());//4//日System.out.println(nowDateTime.getDayOfMonth());//25//时System.out.println(nowDateTime.getHour());//18//分System.out.println(nowDateTime.getMinute());//40//秒System.out.println(nowDateTime.getSecond());//32//纳秒System.out.println(nowDateTime.getNano());//776723800//日:当年的第几天System.out.println("dayofYear:" + nowDateTime.getDayOfYear());//dayofYear:115//星期System.out.println(nowDateTime.getDayOfWeek());//FRIDAYSystem.out.println(nowDateTime.getDayOfWeek().getValue());//5//月份System.out.println(nowDateTime.getMonth());//APRILSystem.out.println(nowDateTime.getMonth().getValue());//4//转化LocalDate ld = nowDateTime.toLocalDate();System.out.println(ld);//2025-04-25LocalTime lt = nowDateTime.toLocalTime();System.out.println(lt.getHour());//18System.out.println(lt.getMinute());//40System.out.println(lt.getSecond());//32}
}

工具类

作用:

Period:用于计算两个“日期”间隔(年、月、日)

Duration:用于计算两个“时间”间隔(秒,纳秒)
ChronoUnit:用于计算两个“日期”间隔

代码演示:

Period:
import java.time.LocalDate;
import java.time.Period;public class Test13 {public static void main(String[] args) {//当前本地 年月日LocalDate today = LocalDate.now();System.out.println(today);//2025-04-25//生日的 年月日LocalDate birthDate = LocalDate.of(2000, 1, 1);System.out.println(birthDate);//2000-01-01Period period = Period.between(birthDate, today);//第二个参数减第一个参数System.out.println("相差的时间间隔对象:" + period);//相差的时间间隔对象:P25Y3M24DSystem.out.println(period.getYears());//25System.out.println(period.getMonths());//3System.out.println(period.getDays());//24//间隔总月份System.out.println(period.toTotalMonths());//303}
}
Duration:
import java.time.Duration;
import java.time.LocalDateTime;public class Test14 {public static void main(String[] args) {//本地日期时间对象。LocalDateTime today = LocalDateTime.now();System.out.println(today);//2025-04-25T18:56:24.858802500//出生的日期时间对象LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1, 0, 0, 0);System.out.println(birthDate);//2000-01-01T00:00Duration duration = Duration.between(birthDate, today);//第二个参数减第一个参数System.out.println("相差的时间间隔对象:" + duration);//相差的时间间隔对象:PT221922H56M24.8588025S//两个时间差的天数System.out.println(duration.toDays());//9246//两个时间差的小时数System.out.println(duration.toHours());//221922//两个时间差的分钟数System.out.println(duration.toMinutes());//13315376//两个时间差的毫秒数System.out.println(duration.toMillis());//798922584858//两个时间差的纳秒数System.out.println(duration.toNanos());//798922584858802500}
}
ChronoUnit:(最常用)
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;public class Test15 {public static void main(String[] args) {//当前时间LocalDateTime today = LocalDateTime.now();System.out.println(today);//2025-04-25T19:03:48.048897900//生日时间LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1,0, 0, 0);System.out.println(birthDate);//2000-01-01T00:00System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today));System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today));System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today));System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today));System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today));System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today));System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today));System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today));System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today));System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today));System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today));System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today));System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today));System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/77145.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java与Kotlin在Android开发中的全面对比分析

趋势很重要 语言发展背景与现状 Android操作系统自2008年正式发布以来&#xff0c;Java长期作为其主要的开发语言。这种选择源于Java语言的跨平台特性、成熟的生态系统以及广泛开发者基础。然而&#xff0c;随着移动开发需求的快速演变&#xff0c;Java在Android开发中逐渐暴…

第一部分:git基本操作

目录 1、git初识 1.1、存在的问题 1.2、版本控制器 1.3、git安装 1.3.1、CentOS平台 1.3.2、ubuntu平台 2、git基本操作 2.1、创建仓库 2.2、配置git 3、工作区、暂存区、版本库 4、基本操作 4.1、场景一 4.2、场景二 4.3、修改文件 5、版本回退 6、撤销修改 …

正则表达式与python使用

一、Python正则表达式基础 1. 导入模块 Python通过 re 模块实现正则表达式功能&#xff0c;需先导入模块&#xff1a; import re2. 核心语法 普通字符&#xff1a;直接匹配字面值&#xff08;如 a 匹配字符 a&#xff09;。元字符&#xff1a; \d&#xff1a;匹配数字&…

从FP32到BF16,再到混合精度的全景解析

笔者做过目标检测模型、超分模型以及扩散生成模型。其中最常使用的是单精度FP32、半精度FP16、BF16。 双精度"FP64"就不说了&#xff0c;不太会用到。 #1. 单精度、半精度和混合精度 单精度&#xff08;FP32&#xff09;、半精度&#xff08;FP16&#xff09;和混合…

Hot100方法及易错点总结2

本文旨在记录做hot100时遇到的问题及易错点 五、234.回文链表141.环形链表 六、142. 环形链表II21.合并两个有序链表2.两数相加19.删除链表的倒数第n个节点 七、24.两两交换链表中的节点25.K个一组翻转链表(坑点很多&#xff0c;必须多做几遍)138.随机链表的复制148.排序链表 N…

不在同一个局域网的远程桌面连接怎么设置?本地内网计算机让其他网络远程访问6种常用方法

远程桌面是一种重要的技术&#xff0c;它允许用户通过网络远程访问和控制另一台计算机的桌面界面。但是&#xff0c;当被控制端和控制端不在同一个局域网内时&#xff0c;就需要进行一些额外的配置。本文将详细介绍在不同局域网下设置远程桌面的步骤&#xff0c;以帮助读者顺利…

天机学堂day10作业,完善兑换优惠券功能

UserCouponServiceImpl /*** 兑换码兑换优惠券* param code*/TransactionalOverridepublic void exchangeCoupon(String code) {//1、校验code是否为空if (StringUtils.isBlank(code)) {throw new BadRequestException("非法参数&#xff01;");}//2、解析兑换码&…

JAVA工程师面试题(七)

1、递归实现1,1,2,3,5,8,….第30个数是多少&#xff1f; public static int Foo(int i) { if (i < 0) return 0; else if(i > 0 && i < 2) return 1; else return Foo(i -1) Foo(i - 2); }…

Qt基础009(HTTP编程和QJSON)

文章目录 软件开发网络架构BS架构/CS架构 HTTP基本概念QT的HTTP编程JSON数据概述QT生成JSON数据QT解析JSON数据 软件开发网络架构 BS架构/CS架构 ​ 在计算机网络和软件开发中&#xff0c;CS架构&#xff08;Client-Server Architecture&#xff0c;客户端-服务器架构&#x…

高精度电流检测革命:同轴分流器的创新应用与技术演进

一、精密测量原理与结构创新 基于电磁场分布重构技术的新型同轴分流器&#xff0c;突破了传统电流测量的物理限制。该器件采用三维环形电阻矩阵结构&#xff0c;通过多层级导电环的精密排列&#xff0c;实现了电流路径的涡流自补偿。区别于常规分流器的平板式设计&#xff0c;其…

【使用层次序列构建二叉树(数据结构C)】

使用层次序列构建二叉树&#xff08;C语言实现&#xff09; 在数据结构学习过程中&#xff0c;二叉树的构建方式通常有递归建树&#xff08;前序/中序&#xff09;和层次建树&#xff08;广度优先&#xff09;两种。本文将介绍一种基于辅助队列实现的层次建树方法&#xff0c;并…

设置Rocky Linux盒盖不休眠的3个简单步骤

在 Rocky linux&#xff08;和其他基于 RHEL 的发行版&#xff09;中&#xff0c;当你关闭笔记本电脑的盖子时&#xff0c;默认行为通常是使系统休眠。如果你想更改这一行为&#xff0c;例如&#xff0c;使系统在关闭盖子时只是锁定&#xff0c;你可以按照以下步骤操作&#xf…

WPF的发展历程

文章目录 WPF的发展历程引言起源与背景&#xff08;2001-2006&#xff09;从Avalon到WPF设计目标与创新理念 WPF核心技术特点与架构基础架构与渲染模型关键技术特点MVVM架构模式 WPF在现代Windows开发中的地位与前景当前市场定位与其他微软UI技术的关系未来发展前景 社区贡献与…

【器件专题1——IGBT第1讲】IGBT:电力电子领域的 “万能开关”,如何撑起新能源时代?

一、IGBT 是什么&#xff1f;重新认识这个 “低调的电力心脏” 你可能没听过 IGBT&#xff0c;但一定用过它驱动的设备&#xff1a;家里的变频空调、路上的电动汽车、屋顶的光伏逆变器&#xff0c;甚至高铁和电网的核心部件里&#xff0c;都藏着这个 “电力电子开关的瑞士军刀”…

新闻速递丨Altair 与 Databricks 达成合作,加速数据驱动型创新

NEWS Altair 近日宣布与数据和人工智能公司 Databricks 达成战略合作&#xff0c;通过新一代数据统一化、图谱驱动智能和企业级人工智能&#xff08;AI&#xff09;技术赋能双方客户。 此次合作整合了两大平台的核心优势&#xff0c;将 Altair RapidMiner 平台的强大功能&…

c++11 :智能指针

目录 一 为什么需要智能指针&#xff1f; 二 智能指针的使用及原理 1. RAII 2. auto_ptr 3. unique_ptr 4. shared_ptr 5. weak_ptr 三 内存泄漏 1.什么是内存泄漏&#xff0c;内存泄漏的危害 2. 如何避免内存泄漏&#xff1f; 一 为什么需要智能指针&#xff1f; …

大模型在直肠癌预测及治疗方案制定中的应用研究

目录 一、引言 1.1 研究背景与意义 1.2 研究目的 1.3 研究方法与创新点 二、大模型技术概述 2.1 大模型的基本原理 2.2 常见大模型类型及特点 2.3 在医疗领域的应用进展 三、直肠癌预测相关数据收集与处理 3.1 数据来源 3.2 数据清洗与预处理 3.3 特征工程 四、大…

VRRP与防火墙双机热备实验

目录 实验一&#xff1a;VRRP负载均衡与故障切换 实验拓扑​编辑一、实验配置步骤 1. 基础网络配置 2. VRRP双组配置 二、关键验证命令 1. 查看VRRP状态 2. 路由表验证 三、流量分析 正常负载均衡场景&#xff1a; 故障切换验证&#xff1a; 实验二&#xff1a;防火…

OpenCV中的SIFT特征提取

文章目录 引言一、SIFT算法概述二、OpenCV中的SIFT实现2.1 基本使用2.1.1 导入库2.1.2 图片预处理2.1.3 创建SIFT检测器2.1.4 检测关键点并计算描述符2.1.5 检测关键点并计算描述符并对关键点可视化2.1.6 印关键点和描述符的形状信息 2.2 参数调优 三、SIFT的优缺点分析3.1 优点…

【信息系统项目管理师】高分论文:论成本管理与采购管理(信用管理系统)

更多内容请见: 备考信息系统项目管理师-专栏介绍和目录 文章目录 论文1、规划成本管理2、成本估算3、成本预算4、成本控制论文 2019年1月,我作为项目经理参与了 XX基金管理有限公司信用管理系统项目。该项目成 本1000万,建设期为1年。通过该项目,XX基金管理有限公司在信用…