Android(安卓)UTC和Local 时间互转
16lz
2022-04-24
public static String getUTCFromLocalTime_(int hour, int minute, int second) { StringBuffer UTCTimeBuffer = new StringBuffer(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // 1、取得本地时间: Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hour, minute, second); // 2、取得时间偏移量: int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET); // 3、取得夏令时差: int dstOffset = cal.get(java.util.Calendar.DST_OFFSET); Log.d("xxx", "getUTCFromLocalTime ZONE_OFFSET : " + zoneOffset + " ,DST_OFFSET: " + dstOffset + ",TimeZone:" + TimeZone.getDefault() + ",Local:" + Locale.getDefault()); // 4、从本地时间里扣除这些差量,即可以取得UTC时间: cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); int yearCal = cal.get(Calendar.YEAR); int monthCal = cal.get(Calendar.MONTH); int dayCal = cal.get(Calendar.DAY_OF_MONTH); int hourCal = cal.get(Calendar.HOUR_OF_DAY); int minuteCal = cal.get(Calendar.MINUTE); int secondCal = cal.get(Calendar.SECOND); UTCTimeBuffer .append(yearCal) .append("-") .append(monthCal) .append("-") .append(dayCal) .append(" ") .append(hourCal < 10 ? ("0" + hourCal) : hourCal) .append(":") .append(minuteCal < 10 ? ("0" + minuteCal) : minuteCal) .append(":") .append(secondCal); try { format.parse(UTCTimeBuffer.toString()); Log.i("", "UTC time-->" + UTCTimeBuffer.toString()); return UTCTimeBuffer.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } public static String getLocalTimeFromUTC_(int hour, int minute, int second) { StringBuffer UTCTimeBuffer = new StringBuffer(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // 1、取得本地时间: Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hour, minute, second); // 2、取得时间偏移量: int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET); // 3、取得夏令时差: int dstOffset = cal.get(java.util.Calendar.DST_OFFSET); Log.d("xxx", "getUTCFromLocalTime ZONE_OFFSET : " + zoneOffset + " ,DST_OFFSET: " + dstOffset + ",TimeZone:" + TimeZone.getDefault() + ",Local:" + Locale.getDefault()); // 4、从UTC时间里加上这些差量,即可以取得本地时间: cal.add(java.util.Calendar.MILLISECOND, (zoneOffset + dstOffset));// System.out.println("p: " + cal.getTimeInMillis());// System.out.println("UTC:" + new Date(cal.getTimeInMillis())); int yearCal = cal.get(Calendar.YEAR); int monthCal = cal.get(Calendar.MONTH); int dayCal = cal.get(Calendar.DAY_OF_MONTH); int hourCal = cal.get(Calendar.HOUR_OF_DAY); int minuteCal = cal.get(Calendar.MINUTE); int secondCal = cal.get(Calendar.SECOND); UTCTimeBuffer .append(yearCal) .append("-") .append(monthCal) .append("-") .append(dayCal) .append(" ") .append(hourCal < 10 ? ("0" + hourCal) : hourCal) .append(":") .append(minuteCal < 10 ? ("0" + minuteCal) : minuteCal) .append(":") .append(secondCal); try { format.parse(UTCTimeBuffer.toString()); Log.i("", "localTime:" + UTCTimeBuffer.toString()); return UTCTimeBuffer.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
以下转载:https://blog.csdn.net/Burn_yourself/article/details/71195241
/** * 当地时间 ---> 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时间 * @return */ public static String Local2UTC(String localTime){ SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//当地时间格式 localFormater.setTimeZone(TimeZone.getDefault()); Date gpsLocalDate = null; try { gpsLocalDate = localFormater.parse(localTime); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC时间格式 utcFormater.setTimeZone(TimeZone.getTimeZone("UTC")); String UTCTime = utcFormater.format(gpsLocalDate.getTime()); return UTCTime; } /** * 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; }
更多相关文章
- 【转】如何获取Android系统时间是24小时制还是12小时制
- 联系人头像 android
- API 25 (Android(安卓)7.1.1 API) widget.ProgressBar——属性分
- android string.xml %问题。
- android 打开文件
- Android用Apache HttpClient 实现POST和Get请求
- 【总结备用】Android(安卓)获取正在运行的任务和服务
- android 获取root修改系统时间
- android http请求并解析返回的xml