JDK8开始新增的日期和时间

ops/2024/10/22 14:43:57/

1、为什么要用JDK8开始新增的时间表:

传统的时间类(Date,simpleDateFormat,Calender)存在如下问题:

①设计不合理,使用不方便,很多都被淘汰了

②都是可变对象,修改后会丢失最开始的时间信息

③线程不安全

④不能精确的纳秒,只能精确的毫秒

1秒=1000毫秒;1毫秒=1000微秒;1微秒=1000纳秒。

localDate、localTime、localDateTime(代替Calender)

localDate代表本地日期(年、月、日、星期)
localTime代表本地时间(时、分、秒、纳秒)
localDateTime代表本地的日期、时间(年、月、日、星期、时、分、秒、纳秒)

他们获取对象的方案

方法名示例
public static xxx now () ;获取系统当前时间对应的该对象

localDate  ld=localDate.now();

localTime lt=localTime.now();

localDateTime ldt=localDateTime.now();

import java.time.LocalDate;public class Test1 {public static void main(String[] args) {//获取本地的日期对象LocalDate ld= LocalDate.now();//年月日System.out.println(ld);//获取日期对象中的信息int year =ld.getYear();//年int month=ld.getMonthValue();//月int day=ld.getDayOfMonth();//日int dayofweek=ld.getDayOfWeek().getValue();//星期几int dayofyear=ld.getDayOfYear();//一年中的第几天System.out.println(year);System.out.println(month);System.out.println(day);System.out.println(dayofweek);System.out.println(dayofyear);//2、直接修改某个信息:withyear,withmonth,withdayofmonth,withdayofyearLocalDate ld1=ld.withYear(1998);System.out.println(ld1);System.out.println(ld);//3、把某个信息加多少:pulsyears,pulsmonths,pulsdays,pulsweeksLocalDate ld2=ld.plusDays(122);System.out.println(ld);System.out.println(ld2);//4、把某个信息减多少:minusyears,minusmonths,minusdays,minusweeksLocalDate ld3=ld.minusMonths(2);System.out.println(ld3);System.out.println(ld);//5、获取指定日期的localDate对象:public static localDate of(int year,int month,int dayofmonth)LocalDate ld4=LocalDate.of(2024,12,12);LocalDate ld5=LocalDate.of(2025,01,01);//6、判断两个日期对象是否相等,在前还是在后:equals isBefore isAfterSystem.out.println(ld4.equals(ld5));System.out.println(ld4.isAfter(ld5));System.out.println(ld4.isBefore(ld5));//7、可以把localDateTime转换成localDate,LocalTime//public localDate toLocalDate();//public localTime toLocalTime();//public  static localDateTime  of(LocalDate date,localTime time);}
}

ZoneId:时区

ZonedDateTime:带时区时间

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.TimeZone;public class ZoneTime {public static void main(String[] args) {//了解时区和带时区的时间//1、Zoneid的常见方法//public static Zoneid systemDefault():获取系统默认的时区ZoneId zoneId=ZoneId.systemDefault();System.out.println(zoneId.getId());System.out.println(zoneId);//public static set<String> getAvaiableZoneIds();获取Java支持的所有时区IdsSystem.out.println(ZoneId.getAvailableZoneIds());//public static ZoneId of(Strig ZoneId):把某个时区id封装成ZoneID对象ZoneId zoneId1=ZoneId.of("America/Cuiaba");//ZoneDateTime带时区时间//public static ZoneDateTime now(ZoneId zone):获取某个时区的ZoneDateTime对象ZonedDateTime now =ZonedDateTime.now(zoneId1);//世界标准时间ZonedDateTime now1=ZonedDateTime.now(Clock.systemUTC());System.out.println(now1);System.out.println(now);//public static ZoneDateTime now():获取系统默认时区的ZoneDateTime对象ZonedDateTime now2=ZonedDateTime.now();System.out.println(now2);Calendar instance=Calendar.getInstance(TimeZone.getTimeZone(zoneId1));System.out.println(instance);//返回美国的时间}
}

Instant:时间线上的某个时刻/时间戳(代替Date)

通过获取Instant对象可以获取此刻的时间,改时间是由两部分组成:从1970年01月01日 00:00:00 开始走到此刻的总秒数+不够1秒的纳秒数

方法名说明
public static Instant now()

获取当前时间的Instant对象(标准时间)

public long getEpochsecod()获取从1970年1月1日00:00:00开始记录的秒数
public int getNano()从时间线开始,获取从第二个开始的纳秒数
pulsMillis  pulsSeconds  pulsNanos判断系列的方法
minusMillis  minusSeconds  minusNanos减少时间系列的方法
equals  isBefore  isAfter增加时间系列的方法

*作用:

①Instant对象的作用:做代码分析,或者记录用户操作的时间点。
②传统的Date类,只能精确的毫秒,并且是可变对象;

③新增的Instant类,可以精确到纳秒,并且是不可变对象,建议用Instant来代替Date

import java.time.Instant;public class Instance {public static void main(String[] args) {//获取当前时间的Instant对象(标准时间)//不可变对象Instant now=Instant.now();//获取从1970年1月1日00:00:00开始记录的秒数//获取总秒数long second=now.getEpochSecond();System.out.println(second);//从时间线开始,获取从第二个开始的纳秒数//获取不够一秒的纳秒数int nami=now.getNano();System.out.println(nami);Instant now1=now.plusMillis(111);System.out.println(now1);Instant now2=now.minusSeconds(12);System.out.println(now2);//Instant对象的作用:做代码分析,或者记录用户操作的时间点Instant n1=Instant.now();//代码执行....Instant n2=Instant.now();
//传统的Date类,只能精确的毫秒,并且是可变对象;
// 新增的Instant类,可以精确到纳秒,并且是不可变对象,建议用Instant来代替Date}
}

DateTimeFormatter:格式化器,用于时间的格式划,解析,线程安全(代替SimpleDateFormat:线程不安全)

方法名说明
public static DateTimeFormatter  ofpattern(时间格式)获取格式化器对象
public String Formate(时间对象)格式化时间

LocalDateTime 提供的格式化,解析时间的方法

方法名说明
public String formate (DateTimeformatter  formatter)格式化时间
public static LocalDateFormatter(parse charsquence text,DateTimeFormatter  formatter)解析时间
rt java.time.LocalDate;
import java.time.LocalDateTime;public class DateTimeFormatter {public static void main(String[] args) {//创建一个日期时间格式化对象出来java.time.format.DateTimeFormatter formatter= java.time.format.DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");//对日期进行格式化LocalDateTime now=LocalDateTime.now();System.out.println(now);String rs=formatter.format(now);//正向格式化System.out.println(rs);//格式化时间其实还有一种方法String rs1=now.format(formatter);//反向格式化System.out.println(rs1);//解析时间:解析时间一般使用LocalDateTime提供的解析方法来解析String rs2="2020年12月12日 12:12:12";LocalDateTime lds=LocalDateTime.parse(rs2,formatter);System.out.println(lds);}
}

Period(一段时期)

可以用于计算两个LocalDate对象相差的年数,月数,天数

方法名说明
public static period between(LocalDate start,LocalDate end)传入两个日期对象,得到Period对象
public int years()计算隔几年,返回
public int months()计算隔几月,返回
public int days()计算隔几天,返回
import java.time.LocalDate;
import java.time.Period;public class Peroid {public static void main(String[] args) {//掌握period的作用:计算两个LocalDate对象相差的年数,月数,天数//1、创建两个period 对象,封装两个日期对象LocalDate start=LocalDate.of(2022,12,12);LocalDate end=LocalDate.of(2025,1,1);Period period=Period.between(start,end);//通过两个period对象,获取两个日期对象相差的信息int years= period.getYears();int months= period.getDays();int days= period.getMonths();System.out.println(years);System.out.println(days);System.out.println(months);}
}

Duration(持续时间)

可以用于计算两个日期对象相差的天数,小时数,分数,秒数,纳秒数,支持LocalTime,LocalDateTime,Instant等时间。

方法名说明
public static Duration between(开始的时间对象1,截至的时间对象2)传入2个时间对象,得到Duration对象
public long  toDays|()计算隔多少天,返回
public long toHours()计算隔多少小时,返回
public long  toMinutes()计算隔多少分,返回
public long  toSeconds()计算隔多少秒,返回
public long  toMillis()计算隔多少毫秒,返回
public long  toNanos()计算隔多少纳秒,返回
import java.time.LocalDateTime;
public class Duration {public static void main(String[] args) {//得到两个时间对象LocalDateTime lds1 =LocalDateTime.of(2023,4,19,11,11,11);LocalDateTime lds2 =LocalDateTime.of(2026,8,18,12,12,12);//得到Duration对象java.time.Duration duration=java.time.Duration.between(lds1,lds2);//获取两个时间对象间隔的时期long a=duration.toDays();long b=duration.toHours();long c=duration.getNano();long d=duration.toMinutes();long e=duration.getSeconds();long f=duration.toMillis();System.out.println(a);System.out.println(b);System.out.println(c);System.out.println(d);System.out.println(e);System.out.println(f);}
}


http://www.ppmy.cn/ops/7430.html

相关文章

解决程序化刷新EXCEL提示更新外部链接的弹窗问题

解决方法 【信任中心】-> 【消息栏】->勾选如下策略提示 2. 【信任中心】->【外部内容】->启用下面的三项链接 3. 【信任中心】->【宏设置】->启用所有宏

C++ 随机数

在许多情况下&#xff0c;需要生成随机数。关于随机数生成器&#xff0c;有两个相关的函数。一个是 rand()&#xff0c;该函数只返回一个伪随机数。生成随机数之前必须先调用 srand() 函数。 下面是一个关于生成随机数的简单实例。实例中使用了 time() 函数来获取系统时间的秒…

web安全学习笔记(10)

记一下第十四节课的内容。 一、MySQL学习 数据库基本结构&#xff1a;库——表——列——值 在本地打开navicat&#xff0c;连接数据库&#xff0c;新建一个liuyan库、liuyan库下新建一个member表&#xff1a; 在表里随意添加一些数据&#xff1a; 下面我们学习MySQL查询。新…

MATLAB求和函数

语法 S sum(A) S sum(A,“all”) S sum(A,dim) S sum(A,vecdim) S sum(,outtype) S sum(,nanflag) 说明 示例 S sum(A) 返回沿大小大于 1 的第一个数组维度计算的元素之和。 如果 A 是向量&#xff0c;则 sum(A) 返回元素之和。 如果 A 是矩阵&#xff0c;则 sum(A) 将…

今日刷三题(day4):简写单词+dd爱框框+除2!

题目一&#xff1a;简写单词 题目描述&#xff1a; 比如 “College English Test”可以简写成“CET”&#xff0c;“Computer Science”可以简写为“CS”&#xff0c;“I am Bob”简写为“IAB” 输入输出描述&#xff1a; 输入&#xff1a;一个复合单词 输出&#xff1a;输…

Compose 布局

文章目录 Compose 布局ColumnColumn属性使用 RowRow属性使用 BoxBox属性使用 ConstraintLayoutLazyColumnLazyColumn属性使用使用多类型使用粘性标题回到顶部 LazyRowLazyRow属性使用 LazyVerticalGridLazyVerticalGrid属性使用 Compose 布局 Column Compose中的”垂直线性布…

C++语言·内存管理

本节内容比较简单&#xff0c;重点就是new和delete关键字&#xff0c;写的就比较粗糙了 1. C/C内存分布 栈上的变量是即用即销毁&#xff0c;堆上的变量按需申请释放&#xff0c;静态和全局的生命周期是整个程序 没编译运行起来的代码叫“程序”&#xff0c;编译运行起来的程序…

String、StringBuilder、StringBuffer区别;String底层详解,实例化、拼接、比较;String为什么不可变

文章目录 0、前言一、String、StringBuilder、StringBuffer区别二、String简介2.1 String类特点2.2 创建String对象、String实例化2.2.1 实例化方法2.2.2 String str1"abc"和String str2new String("abc")区别 2.3 String的比较2.4 String类的“加法”2.5 …