localdatetime所产生的时间如何转换为时间戳呢?
下文笔者讲述LocalDatetime和时间戳相互转换的方法分享,如下所示
时间戳和Localdatetime简介
Unix时间戳(Unix timestamp): 又称之为“Unix时间(Unix time)”或“POSIX时间(POSIX time)” 时间戳在计算中可以用于表示时间 时间戳指:从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数 时间戳的精确数值到秒 LocalDatetime: 是一个不含时区信息的日期时间数据 LocalDatetime在计算机中也是线程安全 那么这两者之间都是没有时区信息,如何实现两者互相转换呢? 下文笔者将一一道来,如下所示
LocalDatetime和时间戳互相转换的示例
//使用LocalDatetime获取时间戳 LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); //时间戳转为LocalDateTime //1.定义一个时间戳 Long timestamp = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); //2.时间戳转换为datetime LocalDateTime time2 =LocalDateTime.ofEpochSecond(timestamp/1000,0,ZoneOffset.ofHours(8));
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。