格林威治時間轉化北京時間以及時間轉換格式代碼大全


格林威治時間與北京時間的相互轉換,后台服務器是格林威治的時間沒有處理就丟給我了,

解決吧,網上一搜,發現這個問題在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文件也打包了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM