Android UTC和Local 时间互转

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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值