Skip to content

Instantly share code, notes, and snippets.

@nhancv
Last active January 28, 2018 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/b8a12b4881d01fad144e35549928bd4b to your computer and use it in GitHub Desktop.
Save nhancv/b8a12b4881d01fad144e35549928bd4b to your computer and use it in GitHub Desktop.
Convert TimeZone [From UTC to LocalTime without Daylight saving (DST)]
try {
//Time formatter input
String dataTime = "2018-01-18 11:59:59";
String dateTimePattern = "yyyy-MM-dd HH:mm:ss";
ZoneId zoneUTC = ZoneId.of("Z");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimePattern).withZone(zoneUTC);
//Get default zone and zone rules
ZoneId zoneCurrent = ZoneId.systemDefault();
ZoneRules rules = zoneCurrent.getRules();
//Parse time
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dataTime, dateTimeFormatter);
//Get Zone offset and convert to current zone without DaylightSaving time (DST)
ZoneOffset zoneOffset = rules.getStandardOffset(zonedDateTime.toInstant());
zonedDateTime = zonedDateTime.plusSeconds(zoneOffset.getTotalSeconds());
System.out.println(zonedDateTime);
} catch (Exception e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment