Java:Instant时间

文章目录

黑马学习笔记

Instant

常用方法

代码

clike 复制代码
package NewTime;

import java.time.Instant;

/**
 * @Author: ggdpzhk
 * @CreateTime: 2024-08-31
 */
public class Test_Instant {
    public static void main(String[] args) {
        // 获取当前时间的Instant对象(标准时间)
        Instant now = Instant.now();
        System.out.println("当前时间:" + now);

        // 获取从1990.1.1 开始的秒数
        long seconds = now.getEpochSecond();
        System.out.println("从1990.1.1 开始的秒数:" + seconds);

        // 获取从1990.1.1开始   不够1s的纳秒数
        long nanos = now.getNano();
        System.out.println("从1990.1.1 开始的纳秒数:" + nanos);

        //传入秒数和纳秒数,得到响应的Instant对象
        Instant instant = Instant.ofEpochSecond(seconds, nanos);
        System.out.println(instant);

        Instant instant1 = now.plusNanos(1000000000);//+1s
        System.out.println(instant1);


        //Instant对象的作用,做代码的性能分析,或者记录用户的操作时间点
        Instant start = Instant.now();
        //代码执行......
        Instant end = Instant.now();

    }
}

当前时间:2024-08-31T05:52:00.824Z:T和Z是什么意思?

用处

  • 可以用来记录代码的执行时间,或者用户操作某个事件的时间点

推荐用Instant类代替Date类

  • 传统的Date类,只能精确到毫秒,并且 是可变对象
  • 新增的Instant类可以精确到纳秒,并且是不可变对象
相关推荐
AutumnWind04206 小时前
【Intelij IDEA使用手册】
java·ide·intellij-idea
codeejun6 小时前
每日一Go-73、云原生成本优化 —— 资源限制 & 指标驱动扩容
开发语言·云原生·golang
就叫_这个吧7 小时前
Java注解、元注解、自定义注解定义及应用
java·开发语言·注解
Sam_Deep_Thinking7 小时前
聊聊Java中的of
java·开发语言·架构
NE_STOP8 小时前
Docker--管理监控平台的应用
java
爱吃羊的老虎8 小时前
【JAVA】python转java:Spring Boot 入门
java·spring boot·python
Love_云宝儿9 小时前
GeoJSON简介
java·gis·地图·jts
摇滚侠9 小时前
JDBC 基础到高级一套通关!进阶篇 16-27
java
明志数科10 小时前
4D时序标注技术详解:让机器人理解连续动作的数据基础
java·算法·机器人
_qingche10 小时前
H2 数据库到 MySQL 数据迁移
java·数据库·spring boot·mysql·spring·重构·kotlin