【Java常用类】Instant:瞬时

Instant:瞬时

时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳,该类型是面向机器的

now():获取本初子午线对应的标准时间

    Instant instant = Instant.now();
    System.out.println(instant);//2019-02-18T07:29:41.719Z

	//添加时间的偏移量
    OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
    System.out.println(offsetDateTime);//2019-02-18T15:32:50.611+08:00

toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数

与Date类的getTime()类似

    long milli = instant.toEpochMilli();
    System.out.println(milli);

ofEpochMilli():通过给定的毫秒数,获取Instant实例

类似Date(long millis)

    Instant instant1 = Instant.ofEpochMilli(1550475314878L);
    System.out.println(instant1);
posted @ 2021-08-12 09:25  ekertree  阅读(147)  评论(0)    收藏  举报