Search code examples
javaparsingdatedatetimeepoch

Convert epoch String to Java date


I am getting an epoch String from my DB which looks something like this : 1391328000000

I am having a hard time trying to convert it to Java Date.

I tried the following :

private String buildDate(String dateString){
        System.out.println("dateString " + dateString);

        DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        String formatted = format.format(Integer.parseInt(dateString));

        return formatted;
    }

Solution

  • I think you're overthinking about the DateFormat. If I want to simply obtain a Date instance, what I would try is the following:

    Date d = new Date(Long.parseLong(dateString));