注意点:
1、UTC 时间格式:yyyy-MM-dd'T'HH:mm:ss.SSS
2、本地时间计算公式:UTC + 时区差 = 本地时间
比如我需要把 UTC 转换成北京时间,北京时间为东八区 +8
所以在计算本地时间的时候需要加上时区差
private final String TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
private final int TIME_ZONE = 8;
private long getTimestamepByUTC(String utcTimeStr) {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
try {
date = sdf.parse(utcTimeStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + TIME_ZONE);
return calendar.getTimeInMillis();
}