java instant_Java Instant类

Instant Class : An instantaneous point on the time-line.

Instant对象表示的就是在时间线上的一点。

Instant对象和时间戳是一一对应的。

这个类能干什呢?

1,处理和时间戳相关的

2,但是不处理 年月日这种单位

作用1 获得当前时间的毫秒的时间戳

Instant timestamp = Instant.now();

long ts = timestamp.toEpochMilli();

既然Instant类是处理时间戳的,肯定可以用ts构造一个Instant对象

ts 转为 Instant对象

long ts2 = 1584700633000L;

Instant instant = Instant.ofEpochMilli(ts2);

LocalDateTime ldt3 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

System.out.println(ldt3);

Instant类还提供了一些常量,如

EPOCH 是一个常量,也就是 时间戳为0的时刻。代表纪元的开始, 以后的时间戳是正数,之前的时间戳是负数。

MIN:Instant能表示的最早的时间

MAX:Instant能表示的最晚的时间

// 时间移动计算,加1小时, 减5天等

// 一小时以后

Instant timestamp = Instant.now();

Instant oneHourLater = timestamp.plus(1, ChronoUnit.HOURS);

LocalDateTime ldt = LocalDateTime.ofInstant(oneHourLater, ZoneId.systemDefault());

System.out.printf("%s %d %d at %d:%d%n", ldt.getMonth(), ldt.getDayOfMonth(),

ldt.getYear(), ldt.getHour(), ldt.getMinute());

// 两天前

Instant timestamp = Instant.now();

Instant twoDaysAgo = timestamp.minus(2, ChronoUnit.DAYS);

LocalDateTime ldt2 = LocalDateTime.ofInstant(twoDaysAgo, ZoneId.systemDefault());

System.out.printf("%s %d %d at %d:%d%n", ldt2.getMonth(), ldt2.getDayOfMonth(),

ldt2.getYear(), ldt2.getHour(), ldt2.getMinute());

因为Instant类是不处理 年月日这种人类的时间单位,如果要处理,需要把 Instant对象转换为 LocalDateTime 或者 ZonedDateTime 这时需要和一个时区绑定。

Instant timestamp;

LocalDateTime ldt = LocalDateTime.ofInstant(timestamp, ZoneId.systemDefault());

System.out.printf("%s %d %d at %d:%d%n", ldt.getMonth(), ldt.getDayOfMonth(),

ldt.getYear(), ldt.getHour(), ldt.getMinute());

代码:

package com.example.demo;

import static java.time.Instant.EPOCH;

import static java.time.Instant.MAX;

import static java.time.Instant.MIN;

import java.time.Instant;

import java.time.LocalDateTime;

import java.time.ZoneId;

import java.time.ZonedDateTime;

import java.time.temporal.ChronoUnit;

public class InstantT {

public static void main(String[] args) {

// 获取当前时间戳 Instant timestamp = Instant.now();

long ts = timestamp.toEpochMilli();

// ts 转为 Instant对象 long ts2 = 1584700633000L;

Instant instant = Instant.ofEpochMilli(ts2);

// instant 可以转化为LocalDateTime 表示当地的时间 LocalDateTime ldt3 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

System.out.println(ldt3);

// LocalDateTime 可以进一步转化为ZonedDateTime,加上了时区的信息 ZonedDateTime zonedDateTime = ZonedDateTime.of(ldt3, ZoneId.systemDefault());

// ZonedDateTime 又可以转换从Instant Instant l = zonedDateTime.toInstant();

System.out.println(l);

boolean equals = instant.equals(l);

System.out.println(equals);

Instant epoch = EPOCH;

System.out.println(epoch);

System.out.println(MIN); //-1000000000-01-01T00:00:00Z System.out.println(MAX); //+1000000000-12-31T23:59:59.999999999Z

// 一小时以后 Instant oneHourLater = timestamp.plus(1, ChronoUnit.HOURS);

LocalDateTime ldt = LocalDateTime.ofInstant(oneHourLater, ZoneId.systemDefault());

System.out.printf("%s %d %d at %d:%d%n", ldt.getMonth(), ldt.getDayOfMonth(),

ldt.getYear(), ldt.getHour(), ldt.getMinute());

// 两天前 Instant twoDaysAgo = timestamp.minus(2, ChronoUnit.DAYS);

LocalDateTime ldt2 = LocalDateTime.ofInstant(twoDaysAgo, ZoneId.systemDefault());

System.out.printf("%s %d %d at %d:%d%n", ldt2.getMonth(), ldt2.getDayOfMonth(),

ldt2.getYear(), ldt2.getHour(), ldt2.getMinute());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值