Skip to content

Instantly share code, notes, and snippets.

@mvervuurt
Created September 23, 2016 20:23
Show Gist options
  • Save mvervuurt/1e661d7463ab3cfc9728412481675549 to your computer and use it in GitHub Desktop.
Save mvervuurt/1e661d7463ab3cfc9728412481675549 to your computer and use it in GitHub Desktop.
Convert GMT UTC EpochSec to ZonedDateTime in chosen timezone
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