Unix epoch time to Java Date object


What is epoch time?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but ‘epoch’ is often used as a synonym for ‘Unix time’. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).

We need to parse the epoch string to long, as it is too large for an int and then multiply the value by 1000 and the pass it to java.util.Date constructor to convert it to java.util.Date object.

package com.jkoder;
import java.util.Date;

public class EpochToDate {
	public static void main(String[] args) {
		String epochString = "1400049361";
		long epoch = Long.parseLong( epochString );
		Date expiry = new Date( epoch * 1000 );