当前位置: 首页 > news >正文

JAVA中时间戳和LocalDateTime的互转

时间戳转LocalDateTime:

要将时间戳转换为LocalDateTime并将LocalDateTime转换回时间戳,使用Java的java.time包。以下是示例代码:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;public class TimestampToLocalDateTime {public static void main(String[] args) {// 时间戳long timestamp = 1692948472; // 使用Instant从时间戳创建时间点Instant instant = Instant.ofEpochSecond(timestamp);// 使用ZoneId定义时区(可以根据需要选择不同的时区)ZoneId zoneId = ZoneId.of("Asia/Shanghai");// 将Instant转换为LocalDateTimeLocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();System.out.println("时间戳: " + timestamp);System.out.println("转换后的LocalDateTime: " + localDateTime);}
}

LocalDateTime转时间戳:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;public class LocalDateTimeToTimestamp {public static void main(String[] args) {// 创建一个LocalDateTime对象LocalDateTime localDateTime = LocalDateTime.of(2023, 8, 25, 0, 0);// 使用ZoneId定义时区(可以根据需要选择不同的时区)ZoneId zoneId = ZoneId.of("Asia/Shanghai");// 将LocalDateTime转换为InstantInstant instant = localDateTime.atZone(zoneId).toInstant();// 获取时间戳long timestamp = instant.getEpochSecond();System.out.println("LocalDateTime: " + localDateTime);System.out.println("转换后的时间戳: " + timestamp);}
}
http://www.lryc.cn/news/139370.html

相关文章:

  • 无涯教程-进程 - 创建终止
  • LLMs参考资料第一周以及BloombergGPT特定领域的训练 Domain-specific training: BloombergGPT
  • LeetCode字符串数组最长公共前缀
  • Git gui教程---第八篇 Git gui的使用 创建一个分支
  • Docker修改daemon.json添加日志后无法启动的问题
  • QT6编译的文件分布情况
  • 2023中国算力大会 | 中科驭数加入DPU推进计划,探讨DPU如何激活算网融合新基建
  • leetcode 115. 不同的子序列
  • gradio应用transformer模块部署生成式人工智能应用程序
  • 【目标检测】“复制-粘贴 copy-paste” 数据增强实现
  • 深度学习知识总结2:主要涉及深度学习基础知识、卷积神经网络和循环神经网络
  • Spring Boot 集成 WebSocket 实现服务端推送消息到客户端
  • vr游乐场项目投资方案VR主题游乐馆互动体验
  • chrom扩展开发配合百度图像文字识别实现自动登录(后端.net core web api)
  • 香港服务器怎么打开SSH
  • 【LeetCode】437.路径总和Ⅲ
  • Mybatis-plus中操作JSON字段
  • 第十五课、Windows 下打包发布 Qt 应用程序
  • 【php】windows下php运行已有php web项目环境配置教程
  • 【mybatis】 mybatis在mysql 更新update 操作 更新时间字段按照年月日时分秒格式 更新为当前时间...
  • C++动态规划经典案例解析之合并石子
  • go MongoDB
  • 算法与数据结构(八)--优先队列
  • React 全栈体系(三)
  • 腾讯云下一代CDN -- EdgeOne加速MinIO对象存储
  • GitLab-CI 指南
  • MyBatis的核心技术掌握,简单易懂(上)
  • Redisson自定义序列化
  • MongoDB Long 类型 shell 查询
  • 回归预测 | MATLAB实现GA-APSO-IBP改进遗传-粒子群算法优化双层BP神经网络多输入单输出回归预测