Created
September 23, 2016 20:23
-
-
Save mvervuurt/1e661d7463ab3cfc9728412481675549 to your computer and use it in GitHub Desktop.
Convert GMT UTC EpochSec to ZonedDateTime in chosen timezone
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
import java.time.Instant; | |
import java.time.ZoneId; | |
import java.time.ZonedDateTime; | |
import java.time.format.DateTimeFormatter; | |
/** | |
* Timezone Conversion Utils between EpochSec Timestamps | |
* and other date formats | |
*/ | |
public class TimeZoneUtils { | |
/** | |
* Convert from UTC EpochSeconds to String DateTime in chosen Timezone | |
* @param epochSec Epoch Seconds Unix Timestamp | |
* @param timezone ZoneId Timezone | |
* @return String DateTime in chosen Timezone | |
*/ | |
public static String toZonedDateTime(long epochSec, String timezone){ | |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | |
ZoneId zoneId = ZoneId.of(timezone); | |
Instant instant = Instant.ofEpochSecond(epochSec); | |
ZonedDateTime tzDt = ZonedDateTime.ofInstant(instant, zoneId); | |
return tzDt.format(formatter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment