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;    }

 

更多相关文章

  1. 【转】如何获取Android系统时间是24小时制还是12小时制
  2. 联系人头像 android
  3. API 25 (Android(安卓)7.1.1 API) widget.ProgressBar——属性分
  4. android string.xml %问题。
  5. android 打开文件
  6. Android用Apache HttpClient 实现POST和Get请求
  7. 【总结备用】Android(安卓)获取正在运行的任务和服务
  8. android 获取root修改系统时间
  9. android http请求并解析返回的xml

随机推荐

  1. scrollView的fillviewport
  2. android listview 点击事件失效
  3. Android Action Bar
  4. android density
  5. 系出名门 Android源代码
  6. 更改RadioButton的背景
  7. 在android 中使用og4j
  8. Android 侧滑删除功能
  9. Android(Java):按钮复选框点中效果
  10. android TV-Working with Channel Data