JDK 1.8中Instant时间戳类,Duration类,Period类,等一些与事件相关的类和接口

本文详细介绍了JDK 1.8中与时间处理相关的类,包括Instant时间戳类的now()、getEpochSecond()等方法,Duration类用于计算时间间隔,Period类用于计算日期间隔,TemporalAdjuster接口作为时间校正器,以及DateTimerFormatter的日期格式化和ZoneID世界时区类的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Instant时间戳类

(一)含义:从1970-01-01 00:00:00到当前时间的毫秒值

(二)常用的方法

 now():

获取当前的时间,获取的是当前的美国时间,和处于东八区的我们相差八个小时。

Instant ins=Instant.now();
System.out.println(ins);
atOffset():

设置偏移量例如:

OffsetDateTime time=ins.atOffset(ZoneOffset.ofHours(8));
System.out.println(time);
atZone()

获取系统默认时区时间,参数为一个时区的编号,可以通过时区编号类获取出来

还可以通过Zoneld.systemDefault()来获取本地的默认时区ID

ZonedDateTime zoneDateTime=ins.atZone(Zoneld.systemDefault());
System.out.println(zonedDateTime);
get系列的方法

getEpochSecond():获取从1970-01-01  00:00:00到当前时间的秒值

getNano():把获取到的当前时间的描述换算成纳秒

ofEpochSecond()方法:在计算机元年(1970-01-01  00:00:00)的基础上增加秒数

Instant instant=Onstant.ofEpochSecond(5);
System.out.println(instant)

Duration类

用法:用于计算两个时间间隔的类

常用方法:
between():计算两个时间的间隔,默认的单位是秒

例:Duration between =Duration.between(start,end);

toMillis()方法:将秒转换成毫秒

System.out.println(between.toMillis());

Period 类

常用方法between():计算两个时间之间的间隔

LocalDate s=LocalDate.of(1985,03,05);
LocalDate now=LocalDate,now();
Periodbe=Period.between(s,now);
System.out.println(be.getYears());
System.out.println(be.getMonth());
System.out.println(be.getDays())

TemporalAdjuster: 时间校正器(接口)

一般我们使用该接口的一个实现类TemporalAdjusters中的一些常量来指定日期。

(1)使用TemporalAdjusters自带的常量来设置日期

LocalDate now=LocalDate.now();
System.out.println(now);
LocalDate with =now.with(TemporalAdjusters.lastDayOdYear());
System.out.println(with);
(2)采用TemporalAdjusters中的next方法来指定日期

  LocalDate date = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(date);

(3)采用自定义的方法来指定日期

 LocalDateTime ldt = LocalDateTime.now();
        LocalDateTime workDay = ldt.with(new TemporalAdjuster() {
public Temporal adjustInto(Temporal temporal) {
               
                LocalDateTime ld = (LocalDateTime) temporal;
                
                DayOfWeek dayOfWeek = ld.getDayOfWeek();
                if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                    return ld.plusDays(3);
                } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                    return ld.plusDays(2);
                } else {
                    
                    return ld.plusDays(1);
                }

DateTimerFormatter

解析和格式化日期或时间

常用方法:

ofPattern("yyyy-MM-dd");:获取对象的静态方法

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime now = LocalDateTime.now();

format():把一个日期对象的默认格式格式化成指定的格式

String format1 = dateFormat.format(now);

System.out.println(format1);

还可以使用日期类中的format 方法传入一个日期格式化类对象。

now1.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);

ZoneID世界时区类

常用方法
getAvailableZonelds():获取世界各个地方的时区的集合
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
ZoneId.systemDefault():获取系统默认时区的ID

  ZoneIdzoneId = ZoneId.systemDefault(); //中国为
Asia/Shanghai







评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值