android中 utc 和 当地时间的转换

xiaoxiao2021-02-28  151

/** * 当地时间 ---> UTC时间 * @return */ public static String Local2UTC(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("gmt")); String gmtTime = sdf.format(new Date()); return gmtTime; } /** * UTC时间 ---> 当地时间 * @param utcTime UTC时间 * @return */ public static String utc2Local(String utcTime) { SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC时间格式 utcFormater.setTimeZone(TimeZone.getTimeZone("UTC")); Date gpsUTCDate = null; try { gpsUTCDate = utcFormater.parse(utcTime); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//当地时间格式 localFormater.setTimeZone(TimeZone.getDefault()); String localTime = localFormater.format(gpsUTCDate.getTime()); return localTime; }
转载请注明原文地址: https://www.6miu.com/read-24107.html

最新回复(0)