Android 获取Local时区Android UTC和Local 时间互转
  HvTJUzsxOBtS 2023年11月19日 17 0


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;
    }
/**
     * 当地时间 ---> 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;
    }
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
HvTJUzsxOBtS