1.通过LocalDateTime
public static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; LocalDateTime ldt = LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern(DATE_FORMAT)); //获得当前Zone的ZonedDateTime=LocalDateTime + ZoneId ZonedDateTime localZoneTime = ldt.atZone(ZoneId.systemDefault()); ZoneId TargetZoneId = ZoneId.of("GMT"); //使用这个方法,就可以得到上一行指定时区的时间 ZonedDateTime TargetDateTime = localZoneTime.withZoneSameInstant(TargetZoneId);DateTimeFormatter format = DateTimeFormatter.ofPattern(DATE_FORMAT); System.out.println("\n---DateTimeFormatter---"); System.out.println("Date (LocalZone) : " + format.format(localZoneTime)); System.out.println("Date (Target) : " + format.format(TargetDateTime));
ZoneId.of("GMT")这里的ZONEID可以通过ZoneId.getAvailableZoneIds()遍历
2.通过TimeZone来指定时区
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("US/Central"));
DateTime minusDate = DateUtil.parse(simpleDateFormat.format(new Date()));//这里的DateUtil是来自于HuTool
TimeZone.getTimeZone("US/Central")这里的ZONE可以通过TimeZone.getAvailableIDs()来遍历