3

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?

CQM
  • 42,592
  • 75
  • 224
  • 366
  • Day in month: `d` (lower case) – oliholz Mar 11 '14 at 13:03
  • @Reimeus multiplying by 1000 gave me correct dates in my application – CQM Mar 11 '14 at 14:20
  • The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). Check [this page](https://stackoverflow.com/q/7857968/10819573) for an answer using the modern Date-Time API. – Arvind Kumar Avinash May 29 '21 at 09:25

2 Answers2

6

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());
Keppil
  • 45,603
  • 8
  • 97
  • 119
4

I suppose you should multiply your input with 1000 (UNIX timestamp in seconds, but Java j.u.Date in milliseconds).

Edit:

Oh another mistake in your code: The pattern symbol should be d, not D (day-of-year).

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126