1. 程式人生 > >本地時間和格林威治時間轉換

本地時間和格林威治時間轉換

/***
 * 轉成格林威治時間
 *
 * @param LocalDate 時間格式必須是yyyy-MM-dd HH:mm:ss
 * @return
 */
public static 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; } /*** * 將當前時間轉成格林威治時間戳 * * @return */ public static String localToGTM() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); format.setTimeZone(TimeZone.getTimeZone("GMT00:00")); try { Date currDate = Calendar.getInstance().getTime(); XLog.e(XLog.TAG_GU
, "轉換前的時間:" + currDate.toString() + " : " + currDate.getTime()); long resultTime = currDate.getTime(); String newTime = format.format(resultTime); XLog.e(XLog.TAG_GU, "轉換後的時間:" + newTime); SimpleDateFormat gmtformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); gmtformat.setTimeZone(TimeZone.getDefault()); Date newDate = gmtformat.parse(newTime); XLog.e(XLog.TAG_GU, "轉換後的時間2 : " + newDate.getTime()); return "" + newDate.getTime(); } catch (Exception e) { e.printStackTrace(); } return format.format(Calendar.getInstance().getTime()); }