Java时间戳互转

编程入门 行业动态 更新时间:2024-10-19 23:41:06

Java<a href=https://www.elefans.com/category/jswz/34/1771441.html style=时间戳互转"/>

Java时间戳互转

企微会议接口时间戳

        Java时间戳转日期和日期转时间戳,10位长度,uint32类型,日期格式自定义,只到日,不到小时,在企微的会议接口会用到,运行结果如下:

当前时间戳:1698026308
10 天后的时间戳:1698883200
转换后的日期:2023-10-23
转换后的日期:2023/11/02

详细代码如下:

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;public class TimeTransfer {public static void main(String[] args) {// 获取当前时间的时间戳Instant currentTimestamp = Instant.now();long currentTimestampInSeconds = currentTimestamp.getEpochSecond();System.out.println("当前时间戳:" + currentTimestampInSeconds);// 获取 10 天后的时间戳LocalDate currentDate = LocalDate.now();LocalDate futureDate = currentDate.plusDays(10);Instant futureTimestamp = futureDate.atStartOfDay().toInstant(ZoneOffset.UTC);long futureTimestampInSeconds = futureTimestamp.getEpochSecond();System.out.println("10 天后的时间戳:" + futureTimestampInSeconds);// 时间戳转日期Instant instantCurr = Instant.ofEpochSecond(currentTimestampInSeconds);Instant instantFuture = Instant.ofEpochSecond(futureTimestampInSeconds);LocalDate dateCurr = instantCurr.atZone(ZoneOffset.UTC).toLocalDate();LocalDate dateFuture = instantFuture.atZone(ZoneOffset.UTC).toLocalDate();// 将 LocalDate 格式化为指定的日期字符串DateTimeFormatter formatterCurr = DateTimeFormatter.ofPattern("yyyy-MM-dd");DateTimeFormatter formatterFuture = DateTimeFormatter.ofPattern("yyyy/MM/dd");String formattedDateCurr = dateCurr.format(formatterCurr);String formattedDateFuture = dateFuture.format(formatterFuture);System.out.println("转换后的日期:" + formattedDateCurr);System.out.println("转换后的日期:" + formattedDateFuture);}
}

更多推荐

Java时间戳互转

本文发布于:2023-12-04 20:40:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1662149.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   Java   戳互转

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!