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

1、local时间转UTC时间

  1. /**
  2. * local时间转换成UTC时间
  3. * @param localTime
  4. * @return
  5. */
  6. public static Date localToUTC(String localTime) {
  7. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  8. Date localDate= null;
  9. try {
  10. localDate = sdf.parse(localTime);
  11. } catch (ParseException e) {
  12. e.printStackTrace();
  13. }
  14. long localTimeInMillis=localDate.getTime();
  15. /** long时间转换成Calendar */
  16. Calendar calendar= Calendar.getInstance();
  17. calendar.setTimeInMillis(localTimeInMillis);
  18. /** 取得时间偏移量 */
  19. int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET);
  20. /** 取得夏令时差 */
  21. int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET);
  22. /** 从本地时间里扣除这些差量,即可以取得UTC时间*/
  23. calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
  24. /** 取得的时间就是UTC标准时间 */
  25. Date utcDate=new Date(calendar.getTimeInMillis());
  26. return utcDate;
  27. }

2、UTC时间转local时间

  1. /**
  2. * utc时间转成local时间
  3. * @param utcTime
  4. * @return
  5. */
  6. public static Date utcToLocal(String utcTime){
  7. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  8. sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
  9. Date utcDate = null;
  10. try {
  11. utcDate = sdf.parse(utcTime);
  12. } catch (ParseException e) {
  13. e.printStackTrace();
  14. }
  15. sdf.setTimeZone(TimeZone.getDefault());
  16. Date locatlDate = null;
  17. String localTime = sdf.format(utcDate.getTime());
  18. try {
  19. locatlDate = sdf.parse(localTime);
  20. } catch (ParseException e) {
  21. e.printStackTrace();
  22. }
  23. return locatlDate;
  24. }

java UTC时间和local时间相互转换的更多相关文章

  1. Java UTC时间与本地时间互相转换

    协调世界时,又称世界统一时间.世界标准时间.国际协调时间.由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC. 这套时间系统被应用于许多互联网和万维网的标准中,例如,网络时间协议就是协 ...

  2. java new Date()得到的时间和系统时间不一样

    造成这种问题的原因是:操作系统时区跟JVM的时区不一致. [root@paas244 ~]# timedatectl Local time: Thu 2016-12-29 15:35:44 CST U ...

  3. 时间:UTC时间、GMT时间、本地时间、Unix时间戳

    转自:http://blog.csdn.net/u012102306/article/details/51538574 1.UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT ...

  4. [转帖]UTC时间、GMT时间、本地时间、Unix时间戳

    UTC时间.GMT时间.本地时间.Unix时间戳 https://www.cnblogs.com/xwdreamer/p/8761825.html 引用: https://blog.csdn.net/ ...

  5. Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数

    格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: /** * 获取指定时间到格林威治时间的秒数 ...

  6. UTC、GTC时间和本地时间

    1.问题 对于装有Windows和Linux系统的机器,进入Windows显示的时间和Linux不一致,Linux中的时间比Windows提前8个小时. 2.解决方法 修改/etc/default/r ...

  7. 计算机程序的思维逻辑 (95) - Java 8的日期和时间API

    ​本节继续探讨Java 8的新特性,主要是介绍Java 8对日期和时间API的增强,关于日期和时间,我们在之前已经介绍过两节了,32节介绍了Java 1.8以前的日期和时间API,主要的类是Date和 ...

  8. Java 中的日期与时间

    Java 日期时间 标签 : Java基础 Date java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含 ...

  9. 第55节:Java当中的IO流-时间api(下)-上

    Java当中的IO流(下)-上 日期和时间 日期类:java.util.Date 系统时间: long time = System.currentTimeMillis(); public class ...

随机推荐

  1. php 函数2

  2. ios数据持久化(转)

    文件系统 归档和序列化 数据库 1.文件系统 不管是Mac OS X 还是iOS的文件系统都是建立在UNIX文件系统基础之上的. 1.1 沙盒模型 在iOS中,一个App的读写权限只局限于自己的沙盒目 ...

  3. Redis (error) NOAUTH Authentication required.解决方法

    当设置redis密码后,打开客户端,需要使用密码验证 auth 123456 就是设置的密码

  4. Okhttp之CallServerInterceptor简单分析

    在Okhttp源码分析专栏的几篇博客分析了Okhttp几个拦截器的主要功能,还剩下最后一个拦截器CallServerInterceptor没有分析,本篇博客就简单分析下该拦截器的功能. 在Okhttp ...

  5. JMeter中各种请求格式--aduocd的博客

    背景:1.在JMeter的HTTP请求的测试中,经常会使用到不同的请求格式.常用的格式如,json,form-data,x-www-form-urlencoded,multipart/form-dat ...

  6. matplotlib.pyplot中add_subplot方法参数111的含义

    下述代码若要运行,得在安装Python之外安装matplotlib.numpy.scipy.six等库,专门来看这篇小贴的朋友应该知道这些库. 参数331的意思是:将画布分割成3行3列,图像画在从左到 ...

  7. spark 与 Hadoop 融合后 Neither spark.yarn.jars nor spark.yarn.archive is set

    参考文献: http://blog.csdn.net/lxhandlbb/article/details/54410644 每次提交Spark任务到yarn的时候,总会出现uploading reso ...

  8. How to convert int [12] to array<int, 12>

    code: // array::data #include <iostream> #include <cstring> #include <array> int m ...

  9. VS2015 LINK : fatal error LNK1264: 已指定 /GENPROFILE 但没有所需的代码生成;检测失败

    C/C++ > 优化 > 全程优化 > 是

  10. 杭电 KazaQ's Socks

    KazaQ wears socks everyday. At the beginning, he has n pairs of socks numbered from 1 to n in his cl ...