Last active
January 28, 2018 08:29
-
-
Save nhancv/b8a12b4881d01fad144e35549928bd4b to your computer and use it in GitHub Desktop.
Convert TimeZone [From UTC to LocalTime without Daylight saving (DST)]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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