javadatesimpledateformatepoch

Java: epoch date to MM/DD/YYYY


My time: 1386696238

My code:

Date date = new Date(Long.parseLong(currentNotification.getDate_created()));
        SimpleDateFormat formatter = new SimpleDateFormat("MM/DD/yyyy", Locale.getDefault());
        String dateString = formatter.format(date);

My result: 1/16/1970

Desired result: 12/10/2013

What am I doing wrong?


Solution

  • Your value is in seconds, so you need to multiply it by 1000.

    Also, DD is day of year, you want dd:

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());