• 格林威治时间转化北京时间以及时间转换格式代码大全


    格林威治时间与北京时间的相互转换,后台服务器是格林威治的时间没有处理就丢给我了,

    解决吧,网上一搜,发现这个问题在10年,甚至08年就有人提出来并解决了,向前人致敬,

    用到了,把有用的总结一下:

    》1 08年有个哥们解决的方式是截取字符串转换格式:

         String ts = "2007-10-23T17:15:44.000Z";   

         System.out.println("ts = " + ts);   

                ts = ts.replace("Z", " UTC");   

                System.out.println("ts = " + ts);   

               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");

                Date dt = sdf.parse(ts);   

               TimeZone tz = sdf.getTimeZone();   

        Calendar c = sdf.getCalendar();   

                System.out.println("Display name: " +   tz.getDisplayName());   

               System.out.println(getString(c));   

    见:http://devsharp.iteye.com/blog/170001

    》2改时区:

      

      public String paserTime(int time){  

        System.setProperty("user.timezone", "Asia/Shanghai");  

           TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");  

       TimeZone.setDefault(tz);  

           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

           String times = format.format(new Date(time * 1000L));  

            System.out.print("日期格式---->" + times);  

               return times;  

      }  

    见:http://blog.csdn.net/xiaanming/article/details/8558547

    》3设置到闰年,时分秒等

    见 :http://blog.sina.com.cn/s/blog_7fdd5eb901013iep.html

    传六位参数,截取判断。

     

    》4 终于找到工具类了

      

    Date nowTime = new Date(); // 要转换的时间
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(nowTime.getTime());

    Log.i("OTH","北京时间:" + cal.getTime().toString().substring(0, 19));

    cal.add(Calendar.HOUR, -8);
    Log.i("OTH","格林威治时间:" + cal.getTime());

    》5 有没有封装还好一点的呢?

    package com.example.mydemo2012;

    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    import java.util.TimeZone;

    class GTMDateUtil {
    /**
    * GTM转本地时间
    *
    * @param GTMDate
    * @return
    */
    @SuppressWarnings("unused")
    public String GTMToLocal(String GTMDate) {
    int tIndex = GTMDate.indexOf("T");
    String dateTemp = GTMDate.substring(0, tIndex);
    String timeTemp = GTMDate.substring(tIndex + 1, GTMDate.length() - 6);
    String convertString = dateTemp + " " + timeTemp;

    SimpleDateFormat format;
    format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    Date result_date;
    long result_time = 0;

    if (null == GTMDate) {
    return GTMDate;
    } else {
    try {
    format.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
    result_date = format.parse(convertString);
    result_time = result_date.getTime();
    format.setTimeZone(TimeZone.getDefault());
    return format.format(result_time);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return GTMDate;
    }

    /***
    * 转成格林威治时间 感觉用不到
    */
    public String LocalToGTM(String LocalDate) {
    SimpleDateFormat format;
    format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    Date result_date;
    long result_time = 0;
    if (null == LocalDate) {
    return LocalDate;
    } else {
    try {
    format.setTimeZone(TimeZone.getDefault());
    result_date = format.parse(LocalDate);
    result_time = result_date.getTime();
    format.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
    return format.format(result_time);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return LocalDate;
    }

    }

    very good!

    见:http://blog.csdn.net/sun6223508/article/details/45189841

    >获取时间大全,格式转换+闰年。。。。++,

    见:http://www.oschina.net/code/snippet_575610_22694

    整理好了,下载地址:

      http://download.csdn.net/detail/onebelowzero2012/9374733

    欢迎补充,另外c写的 获取格林威治时间的exe文件也打包了。

  • 相关阅读:
    bzoj 1800 & 洛谷 P2165 [AHOI2009]飞行棋 —— 模拟
    bzoj 1050 [ HAOI 2006 ] 旅行comf —— 并查集
    洛谷P2593 [ ZJOI 2006 ] 超级麻将 —— DP
    bzoj 3029 守卫者的挑战 —— 概率DP
    poj 2288 Islands and Bridges ——状压DP
    bzoj 1029 [ JSOI 2007 ] 建筑抢修 —— 贪心
    bzoj 3743 [ Coci 2015 ] Kamp —— 树形DP
    bzoj 1053 [ HAOI 2007 ] 反素数ant ——暴搜
    【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
    构造类斐波那契数列矩阵(矩阵
  • 原文地址:https://www.cnblogs.com/yizuochengchi2012/p/5071822.html
Copyright © 2020-2023  润新知