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

建站系统源码黑龙江交通系统网站建设

建站系统源码,黑龙江交通系统网站建设,前端代码生成器,门户建设网站第13章 时间处理 示例代码 当时间存储需要格式化,可用 hutool 工具 LocalDateTimeUtil 进行时间格式的转化。 // 上报时间格式化并且时间在东八区 String eventTimeStr s.getEventTime(); LocalDateTime localDateTime LocalDateTimeUtil.parse(eventTimeStr, &qu…

第13章 时间处理

示例代码

当时间存储需要格式化,可用 hutool 工具 LocalDateTimeUtil 进行时间格式的转化。

// 上报时间格式化并且时间在东八区
String eventTimeStr = s.getEventTime();
LocalDateTime localDateTime = LocalDateTimeUtil.parse(eventTimeStr, "yyyyMMdd'T'HHmmss'Z'");
LocalDateTime eventTime = DateTimeZoneConverter.utcToShanghai(localDateTime);

DateTimeZoneConverter工具类

package com.zzyl.nursing.util;import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Optional;/*** 日期时区转换工具类*/
public class DateTimeZoneConverter {// 预定义常用时区public static final ZoneId UTC_ZONE = ZoneOffset.UTC;public static final ZoneId SHANGHAI_ZONE = ZoneId.of("Asia/Shanghai");// 私有构造防止实例化private DateTimeZoneConverter() {}/*** 转换LocalDateTime时区(明确时区上下文)* @param sourceTime 源时间(无时区信息)* @param sourceZone 源时间所在的时区* @param targetZone 目标时区* @return 转换后的LocalDateTime*/public static LocalDateTime convert(LocalDateTime sourceTime, ZoneId sourceZone,ZoneId targetZone) {return sourceTime.atZone(sourceZone).withZoneSameInstant(targetZone).toLocalDateTime();}/*** 安全转换方法(Optional包装)* @param sourceTime 可为null的源时间* @param sourceZone 源时区* @param targetZone 目标时区* @return Optional包装的转换结果*/public static Optional<LocalDateTime> safeConvert(LocalDateTime sourceTime,ZoneId sourceZone,ZoneId targetZone) {return Optional.ofNullable(sourceTime).map(time -> convert(time, sourceZone, targetZone));}/*** UTC转上海时区的快捷方法* @param utcTime UTC时间的LocalDateTime* @return 上海时区本地时间*/public static LocalDateTime utcToShanghai(LocalDateTime utcTime) {return convert(utcTime, UTC_ZONE, SHANGHAI_ZONE);}/*** 带格式解析的完整流程* @param timeStr 时间字符串* @param pattern 格式模式(需匹配timeStr)* @param sourceZone 字符串对应的时区* @param targetZone 目标时区* @return Optional包装的转换结果*/public static Optional<LocalDateTime> parseAndConvert(String timeStr,String pattern,ZoneId sourceZone,ZoneId targetZone) {try {LocalDateTime sourceTime = LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern));return Optional.of(convert(sourceTime, sourceZone, targetZone));} catch (Exception e) {return Optional.empty();}}
}
http://www.dtcms.com/a/492315.html

相关文章:

  • 万网网站制作学软件开发需要多少钱
  • RHCSA复习练习题
  • SEO vs AI 优化(GEO):跨境电商谁能带来更高流量增长?
  • 网站文件下载系统wordpress 优秀网站
  • 石家庄网站建设排名沈阳网页建站模板
  • 做智慧教室的网站家用电脑进行网站建设
  • 双分布函数热 LBM 模拟二维封闭方腔自然对流
  • 网站搭建 里短信asp wordpress
  • 做公司网站需注意什么装修设计装饰
  • 如何做好接口测试
  • 谈谈你对 Activity.runOnUiThread 的理解?
  • Bootstrap 5 响应式网站首页模板
  • 朝阳区北京网站建设怎么用flash做游戏下载网站
  • 在 FastAPI 项目中集成 FastMCP:完整指南与生命周期管理
  • /dev/null 是什么,有什么用途?
  • 原子核外电子排布的量子规律:从薛定谔方程到电子排布——薛定谔方程在球坐标系下的求解
  • 微信如何做网站网站备案注销
  • 做网站就要租服务器如何新建一个网站
  • MSTP 练习
  • 网站正在建设页面模板装饰公司门头
  • GPT-oss + vLLM + LobalChat
  • 外包网站建设是什么意思超级门户wordpress企业主题
  • 文本增强:回译技术,小数据集怎样扩充?
  • 动态业务需求下的突围——六款AI数据分析工具全景测评
  • MinerU2.5:高分辨率文档解析的解耦式视觉语言模型革命
  • 怎样建设商城网站淘宝店群软件定制开发
  • TDengine 数学函数 GREATEST 用户手册
  • 网站创建服务公司网站建设收费标准方案
  • Ceph: 一个可扩展的高性能分布式文件系统
  • 合规检查:OPA策略,K8s资源合规验证?