本示例演示如何将utc时间(格林威治实际标准时间)转换为北京时间(utc-东八区时间)。

有如下请求参数:
{
    "comeTime": "2020-11-15T00:01:45.000Z",
}

comeTime表示utc时间 2020年11月15日00点01分45秒。需要根据以上时间返回当前北京时间: 2020年11月15日08点01分45秒

@ApiOperation(value = "/getbjtime", notes = "根据utc时间返回utc-8北京时间")
public BaseOutputDto<String> getbjtime( @ApiParam(value = "inputDto",name = "时间参数")@RequestBody InputDto inputDto)
{LocalDateTime comeTime = inputDto.getComeTime();return toUtcBeijing(comeTime);
}public static LocalDateTime toUtcBeijing(LocalDateTime localDateTime){ZonedDateTime zonedtime = localDateTime.atZone(ZoneId.from(ZoneOffset.UTC));ZonedDateTime converted = zonedtime.withZoneSameInstant(ZoneOffset.ofHours(8));return converted.toLocalDateTime();
}

以下分享一个时区处理工具类

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;public final class DateTimeUtil {private DateTimeUtil() {super();}public static void main(final String... args) {final LocalDateTime now = LocalDateTime.now();final LocalDateTime utc = DateTimeUtil.toUtc(now);System.out.println("Now: " + now);System.out.println("UTC: " + utc);}public static LocalDateTime toZone(final LocalDateTime time, final ZoneId fromZone, final ZoneId toZone) {final ZonedDateTime zonedtime = time.atZone(fromZone);final ZonedDateTime converted = zonedtime.withZoneSameInstant(toZone);return converted.toLocalDateTime();}public static LocalDateTime toZone(final LocalDateTime time, final ZoneId toZone) {return DateTimeUtil.toZone(time, ZoneId.systemDefault(), toZone);}public static LocalDateTime toUtc(final LocalDateTime time, final ZoneId fromZone) {return DateTimeUtil.toZone(time, fromZone, ZoneOffset.UTC);}public static LocalDateTime toUtc(final LocalDateTime time) {return DateTimeUtil.toUtc(time, ZoneId.systemDefault());}
}

UTC LocalDateTime 根据时区转换相关推荐

  1. LocalDateTime 时区转换,转指定时区

    需求 服务器在国外,数据传到国内,LocalDateTime获取到的时间是国外时间,需要转换成国内时间,又或者服务器在国内,数据传到国外,需要转换成国外的时间. 直接上代码 /*** 获取指定时区的时 ...

  2. 关于java时区转换夏令时问题及解决办法

    关于中国的夏令时问题:参考另外一篇: https://blog.csdn.net/u011165335/article/details/78924498 这里:主要讲的是: 在夏令时结束的时候,jav ...

  3. Bailian2966 时区转换【时区计算】

    2966:时区转换 总时间限制: 1000ms 内存限制: 65536kB 描述 直到19世纪,时间校准是一个纯粹的地方现象.每一个村庄当太阳升到最高点的时候把他们的时钟调到中午12点.一个钟表制造商 ...

  4. Linux使用带时区转换的时间函数注意事项(负负得正)

    linux时间结构体定义: struct tm {      int    tm_sec; //代表目前秒数,正常范围为0-59,但允许至61秒      int    tm_min; // 代表目前 ...

  5. c语言 北京时间转换utc时间_UTC时间转换成北京时间C语言函数代码

    UTC + 时区差 = 本地时间 时区差东为正,西为负.在此,把东八区时区差记为 +0800, UTC + (+0800) = 本地(北京)时间 (1) 那么,UTC = 本地时间(北京时间))- 0 ...

  6. 东八区转为0时区_Java时区转换及时间格式

    本文介绍Java API 中 Date, Calendar, TimeZone和DateFormat的使用,以及不同时区时间相互转化的方法和原理. 问题描述: 向处于不同时区的服务器发请求时需要考虑时 ...

  7. 几种日期时区转换方式

    常用的时区转换有如下几种方式 方式一 转换原理 通过DateFormat 的format方法 代码示例 import java.text.DateFormat; import java.text.Pa ...

  8. Python3中datetime不同时区转换介绍与踩坑

    最近的项目需要根据用户所属时区制定一些特定策略,学习.应用了若干python3的时区转换相关知识,这里整理一部分记录下来. 下面涉及的几个概念及知识点: GMT时间:Greenwich Mean Ti ...

  9. 客户端、服务器、数据库之间的时区转换

    背景 做国外的项目经常会遇到时区转换的问题,这里简单针对遇到的时区问题做个记录,也希望对大家有所帮助,少走弯路.(本文设计开发语言为java) 时区的概念 先说下时区的概念 初中地理好的同学应该还记得 ...

最新文章

  1. 探究!一个数据包在网络中的心路历程
  2. 罗森伯格成功布线中国海关博物馆
  3. android中字符串转换成整型
  4. 商品评价 - 实现分页
  5. 这部纪录片央视发力了,每一帧都可以当壁纸,BBC也无法超越
  6. gpu版tensorflow测试
  7. 无需人工!无需训练!构建知识图谱 BERT一下就行了!
  8. 廖雪峰--python教程:笔记四-函数式编程
  9. 本地部署Bootstrap3
  10. linux mp4box安装,mp4box安装备忘
  11. linux网络代码结构
  12. 【爬虫系列之三】URLError异常处理以及Cookie的使用
  13. 李航统计学习方法 Chapter1 统计学习方法概论
  14. 布衣联盟装机常用软件光盘——金鼠报春版
  15. python常用方法技巧使用总结
  16. Android Java(2015-6-18 15:28、2016-1-30 21:18、2016-5-31 11:20)
  17. win10安装onnx、tensorrt(python用,超简单安装版)
  18. GitHub分支使用详解
  19. html用九张图片做出九宫图,九宫切图软件 如何快速把照片做成九宫格切图
  20. 5.5leecode刷题记录(leecode704.二分查找,leecode.27移除元素)

热门文章

  1. 流入接地网的电流IG与站内站外发生短路时的电流关系
  2. 电脑桌面计算机打开很慢,我的电脑打开很慢怎么办
  3. winter 老师前端面试直播总结
  4. 校招答疑总结!准备实习、校招的学妹(弟)们,可不要努力错方向了!
  5. Java是什么,有什么优势及特点
  6. 嘉立创EDA原理图封装画错了怎么办
  7. linux系统终端用户名和密码忘记了,主机大师(Linux)登录账户密码忘记的解决办法...
  8. 码、候选码、主键、主属性
  9. 5G移动通信系统设计与标准详解 个人整理(PDF和Word)
  10. 《九》微信小程序中的自定义组件