0

I have TimeStamp time as String 1374160160 which is equivalent 07/18/2013.

If there is a way how I can convert this TimeStamp to date with format July 18, 2013 ?

This is my code snippet:

try{
  String timeStamp = "1374160160";
  DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
  Date netDate = (new Date(timeStamp));
  return sdf.format(netDate);
} catch(Exception ex) {
  return "";
}

It always returns 01/17/1970 why?

The second problem is that new Date(String) is deprecated.

To sum up:

How to create normal date (July 18, 2013) from time-stamp and avoid the deprecated methods ?

VLeonovs
  • 2,193
  • 5
  • 24
  • 44

1 Answers1

1

Your timeStamp instance holds seconds but Java Date constructor expects milliseconds. Via documentation:

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT

So you should use this:

String timeStamp = "1374160160";
long timeMillis = Long.valueOf(timeStamp) * 1000;
Date netDate = new Date(timeMillis);

instead of:

String timeStamp = "1374160160";
Date netDate = (new Date(timeStamp));
Cootri
  • 3,806
  • 20
  • 30