• UTC时间与本地时间转换


    /**
    * UTC时间转成本地时间
    * 把带t和z的时间转换成相应的时间格式(对应时区)
    * @param tzTime 传入的时间(格式为:2016-08-15T16:00:00.000Z)
    * @param foramt 返回的时间类型
    * @return
    */
    public static String UTCChangeToLocal(String tzTime, String foramt) {
      tzTime = tzTime.replace("Z", " UTC");
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
      String res = "";
      try {
        Date d = format.parse(tzTime);
        SimpleDateFormat formats = new SimpleDateFormat(foramt);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        res = formats.format(cal.getTime());
      } catch (Exception e) {
        e.printStackTrace();
      }
      return res;
    }

    /**
    * 本地时间转成UTC时间
    * @param date 本地时间
    * @param Localformat 本地时间格式(例如:yyyyMMdd HH:mm:ss 如果没有后面的HH:mm:ss 这默认为00:00:00)
    * @param returnFormat 要返回的时间格式(yyyyMMdd HH:mm:ss)
    * @return
    */
    public static String LocalChangeToUTC(String date, String localformat, String returnFormat) {
      try {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(returnFormat);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        SimpleDateFormat sdf = new SimpleDateFormat(localformat);
        Date newDate = sdf.parse(date);
        return simpleDateFormat.format(newDate);
      } catch (Exception e) {
        e.printStackTrace();
      }
      return "";
    }

  • 相关阅读:
    在 Linux 上如何挂载 qcow2 磁盘镜像
    CentOS ISO 下载地址
    构建ceph deb 安装包
    ceph 源码安装 configure: error: "Can't find boost spirit headers"
    sudo: 没有终端存在,且未指定 askpass 程序
    ubuntu14.04 下出现 libmysqlclient.so.20 找不到问题
    binary-tree-postorder-traversal leetcode C++
    binary-tree-preorder-traversal leetcode C++
    candy leetcode C++
    clone-graph leetcode C++
  • 原文地址:https://www.cnblogs.com/baimj/p/14095655.html
Copyright © 2020-2023  润新知