java UTC时间和local时间相互转换
java UTC时间和local时间相互转换
1、local时间转UTC时间

- /**
- * local时间转换成UTC时间
- * @param localTime
- * @return
- */
- public static Date localToUTC(String localTime) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date localDate= null;
- try {
- localDate = sdf.parse(localTime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- long localTimeInMillis=localDate.getTime();
- /** long时间转换成Calendar */
- Calendar calendar= Calendar.getInstance();
- calendar.setTimeInMillis(localTimeInMillis);
- /** 取得时间偏移量 */
- int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET);
- /** 取得夏令时差 */
- int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET);
- /** 从本地时间里扣除这些差量,即可以取得UTC时间*/
- calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
- /** 取得的时间就是UTC标准时间 */
- Date utcDate=new Date(calendar.getTimeInMillis());
- return utcDate;
- }

2、UTC时间转local时间

- /**
- * utc时间转成local时间
- * @param utcTime
- * @return
- */
- public static Date utcToLocal(String utcTime){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
- Date utcDate = null;
- try {
- utcDate = sdf.parse(utcTime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- sdf.setTimeZone(TimeZone.getDefault());
- Date locatlDate = null;
- String localTime = sdf.format(utcDate.getTime());
- try {
- locatlDate = sdf.parse(localTime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return locatlDate;
- }
java UTC时间和local时间相互转换的更多相关文章
- Java UTC时间与本地时间互相转换
协调世界时,又称世界统一时间.世界标准时间.国际协调时间.由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC. 这套时间系统被应用于许多互联网和万维网的标准中,例如,网络时间协议就是协 ...
- java new Date()得到的时间和系统时间不一样
造成这种问题的原因是:操作系统时区跟JVM的时区不一致. [root@paas244 ~]# timedatectl Local time: Thu 2016-12-29 15:35:44 CST U ...
- 时间:UTC时间、GMT时间、本地时间、Unix时间戳
转自:http://blog.csdn.net/u012102306/article/details/51538574 1.UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT ...
- [转帖]UTC时间、GMT时间、本地时间、Unix时间戳
UTC时间.GMT时间.本地时间.Unix时间戳 https://www.cnblogs.com/xwdreamer/p/8761825.html 引用: https://blog.csdn.net/ ...
- Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数
格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: /** * 获取指定时间到格林威治时间的秒数 ...
- UTC、GTC时间和本地时间
1.问题 对于装有Windows和Linux系统的机器,进入Windows显示的时间和Linux不一致,Linux中的时间比Windows提前8个小时. 2.解决方法 修改/etc/default/r ...
- 计算机程序的思维逻辑 (95) - Java 8的日期和时间API
本节继续探讨Java 8的新特性,主要是介绍Java 8对日期和时间API的增强,关于日期和时间,我们在之前已经介绍过两节了,32节介绍了Java 1.8以前的日期和时间API,主要的类是Date和 ...
- Java 中的日期与时间
Java 日期时间 标签 : Java基础 Date java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含 ...
- 第55节:Java当中的IO流-时间api(下)-上
Java当中的IO流(下)-上 日期和时间 日期类:java.util.Date 系统时间: long time = System.currentTimeMillis(); public class ...
随机推荐
- php 函数2
- ios数据持久化(转)
文件系统 归档和序列化 数据库 1.文件系统 不管是Mac OS X 还是iOS的文件系统都是建立在UNIX文件系统基础之上的. 1.1 沙盒模型 在iOS中,一个App的读写权限只局限于自己的沙盒目 ...
- Redis (error) NOAUTH Authentication required.解决方法
当设置redis密码后,打开客户端,需要使用密码验证 auth 123456 就是设置的密码
- Okhttp之CallServerInterceptor简单分析
在Okhttp源码分析专栏的几篇博客分析了Okhttp几个拦截器的主要功能,还剩下最后一个拦截器CallServerInterceptor没有分析,本篇博客就简单分析下该拦截器的功能. 在Okhttp ...
- JMeter中各种请求格式--aduocd的博客
背景:1.在JMeter的HTTP请求的测试中,经常会使用到不同的请求格式.常用的格式如,json,form-data,x-www-form-urlencoded,multipart/form-dat ...
- matplotlib.pyplot中add_subplot方法参数111的含义
下述代码若要运行,得在安装Python之外安装matplotlib.numpy.scipy.six等库,专门来看这篇小贴的朋友应该知道这些库. 参数331的意思是:将画布分割成3行3列,图像画在从左到 ...
- spark 与 Hadoop 融合后 Neither spark.yarn.jars nor spark.yarn.archive is set
参考文献: http://blog.csdn.net/lxhandlbb/article/details/54410644 每次提交Spark任务到yarn的时候,总会出现uploading reso ...
- How to convert int [12] to array<int, 12>
code: // array::data #include <iostream> #include <cstring> #include <array> int m ...
- VS2015 LINK : fatal error LNK1264: 已指定 /GENPROFILE 但没有所需的代码生成;检测失败
C/C++ > 优化 > 全程优化 > 是
- 杭电 KazaQ's Socks
KazaQ wears socks everyday. At the beginning, he has n pairs of socks numbered from 1 to n in his cl ...