public class DateUtils { private DateUtils() { } public static final String FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; public static long formatUTCToLong(String utc) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_UTC); Date parse = sdf.parse(utc); Calendar calendar = Calendar.getInstance(); calendar.setTime(parse); calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8); return calendar.getTimeInMillis(); } public static void main(String[] args) throws ParseException { String FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; String utc ="2022-11-15T14:00:08.996Z"; SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_UTC); Date parse = sdf.parse(utc); Calendar calendar = Calendar.getInstance(); calendar.setTime(parse); calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8); System.out.println(formatUTCToLong("2022-11-15T14:00:08.996Z")); System.out.println(formatUTCToLong("2022-11-15T14:00:08.997Z")); System.out.println(formatUTCToLong("2022-11-15T14:00:09.000Z")); }
}
输出效果
1668520808996
1668520808997
1668520809000