安卓UTC与时间日期互转方法

  • 获取时区

 public static long getGmtTimeZone() {
        long _t = TimeZone.getDefault().getOffset(System.currentTimeMillis()) / (3600 * 1000);
        return _t;
    }
  • UTC毫秒转时间日期格式

public static String longtransformstring(Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");

        Date dt = new Date(time);
        String stime = sdf.format(dt);
        return stime;

    }
  • 时间日期转UTC秒

 public static long date_to_utc(int y,int m,int d,int h,int min,int sec){
        Calendar calendar = Calendar.getInstance();
        calendar.set(y,m-1,d,h,min,sec);
        long timezone = getGmtTimeZone();
        long time = calendar.getTimeInMillis()/1000+timezone*3600;
        Log.i("TEST12", "set_alarm_time: " +time);
        return time;
    }

 

 

 

 

你可能感兴趣的:(Anroid学习笔记)