android中 utc 和 当地时间的转换

来源:互联网 发布:tp5框架架构源码下载 编辑:程序博客网 时间:2024/06/13 18:14
   /**     * 当地时间 ---> 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;    }

1 0
原创粉丝点击