//默认为上海时区TimeZone tz = TimeZone.getDefault();//北京时区tz = TimeZone.getTimeZone("GMT+8");Date date = new Date(System.currentTimeMillis());// 获取默认的DateFormat,用于格式化DateSimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");// 设置时区为tzsimpleDateFormat.setTimeZone(tz);// 获取格式化后的字符串String s = "2020-03-17 15:48:40 +0200";Date date1 = simpleDateFormat.parse(s);//将转换后的北京时间转成标准格式SimpleDateFormat basicSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = basicSimpleDateFormat.format(date1);System.out.println(time);
实例代码如上所示