|NO.Z.00040|——————————|BigDataEnd|——|Java&核心类库.V10|——|Java.v10|Install类|概念使用|
一、[Install类的概念和使用]
### --- Instant类的概述
~~~ # 基本概念
~~~ ——> java.time.Instant类主要用于描述瞬间的时间点信息。
二、常用的方法
方法声明 | 功能介绍 |
static Instant now() | 从系统时钟上获取当前时间 |
OffsetDateTimeatOffset(ZoneOffset offset) | 将此瞬间与偏移量组合以创建偏移日期时间 |
static Instant ofEpochMilli(longepochMilli) | 根据参数指定的毫秒数来构造对象, 参数为距离1970年1月1日0时0分0秒的毫秒数 |
long toEpochMilli() | 获取距离1970年1月1日0时0分0秒的毫秒数 |
三、编程代码
package com.yanqi.task13;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class InstantTest {
public static void main(String[] args) {
// 1.使用Instant类来获取当前系统时间 并不是当前系统的默认时区 本初子午线 差8小时 东八区
Instant now = Instant.now();
System.out.println("获取到的当前时间为:" + now);
// 2.加上时区所差的8个小时
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println("偏移后的日期时间为:" + offsetDateTime);
System.out.println("--------------------------------------------------------");
// 3.获取当前调用对象距离标准基准时间的毫秒数
long g1 = now.toEpochMilli();
System.out.println("获取到的毫秒差为:" + g1);
// 4.根据参数指定的毫秒数来构造对象
Instant instant = Instant.ofEpochMilli(g1);
System.out.println("根据参数指定的毫秒数构造出来的对象为:" + instant);
}
}
四、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53585:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task13.InstantTest
获取到的当前时间为:2021-07-30T12:02:58.371292600Z
偏移后的日期时间为:2021-07-30T20:02:58.371292600+08:00
--------------------------------------------------------
获取到的毫秒差为:1627646578371
根据参数指定的毫秒数构造出来的对象为:2021-07-30T12:02:58.371Z
Process finished with exit code 0
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通