I recommend you switch from the outdated and error-prone java.util
date-time API to the rich set of modern date-time API.
import java.text.ParseException;
import java.time.Instant;
public class Main {
public static void main(final String[] args) throws ParseException {
Instant instant = Instant.ofEpochSecond(1593572400);
System.out.println(instant);
}
}
Output:
2020-07-01T03:00:00Z
Once you have an object of Instant
, you can easily convert it to other date/time objects as shown below:
import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(final String[] args) throws ParseException {
Instant instant = Instant.ofEpochSecond(1593572400);
System.out.println(instant);
ZonedDateTime zdt = instant.atZone(ZoneId.of("Europe/London"));
OffsetDateTime odt = zdt.toOffsetDateTime();
LocalDateTime ldt = odt.toLocalDateTime();
System.out.println(zdt);
System.out.println(odt);
System.out.println(ldt);
}
}
Output:
2020-07-01T03:00:00Z
2020-07-01T04:00+01:00[Europe/London]
2020-07-01T04:00+01:00
2020-07-01T04:00
Check this to get an overview of modern date-time classes.