/*** * 转成格林威治时间 * * @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()); }
本地时间和格林威治时间转换
最新推荐文章于 2022-10-07 22:19:04 发布