CST 时间格式减去八小时
问题:
查看服务器时间是正确的,但输出出来的时间,比此时多出来八个小时。这里直接把时间减去八个小时。
public static void main(String[] args) throws ParseException {// 设定原始时间格式try {SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);dateFormat.setLenient(false);// 设置时区,否则会使用系统默认时区dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));// 解析日期字符串Date date = dateFormat.parse("Sun Jun 23 06:10:03 CST 2024");// 获取Calendar实例Calendar calendar = Calendar.getInstance();calendar.setTime(date);// 减去8小时calendar.add(Calendar.HOUR_OF_DAY, -8);// 格式化输出结果Date updatedDate = calendar.getTime();DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(updatedDate));} catch (Exception e) {e.printStackTrace();}}//输出结果:2024-06-22 22:10:03