java.time
Your string, "1329242400"
is the number of seconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
(or UTC). You can convert it into an Instant
using Instant#ofEpochSecond
which represents an instantaneous point on the timeline. It is independent of timezone (i.e. has a timezone offset of 00:00
hours, represented with Z
in ISO 8601 standards).
You can convert Instant
to a ZonedDateTime
by applying the applicable ZoneId
.
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.ofEpochSecond(Long.parseLong("1329242400"));
System.out.println(instant);
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of("America/New_York"));
System.out.println(zdt);
}
}
Output:
2012-02-14T18:00:00Z
2012-02-14T13:00-05:00[America/New_York]
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.