java.text.ParseException: Unparseable date: "531772200000"

1.5k Views Asked by At

I need to convert the string 531772200000 to a Java Date object. The date is stored in a database.

When I am doing this, I am getting java.text.ParseException: Unparseable date: "531772200000".

My code:

String dateToBeConverted = String.valueOf(dbObject.get("customerDateOfBirth"));
String parseabledate = dateToBeConverted
    .replace("/Date(","")
    .replace(")/","")
    .replace("+0530", "");
dbObject.put("_id", String.valueOf(dbObject.get("userInfoId")));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date;
date = formatter.parse(parseabledate);
3

There are 3 best solutions below

0
On BEST ANSWER

This looks like a timestamp value, this will probably give you the date:

new Date(Long.parseLong("531772200000"));

which works out at Fri Nov 07 1986 18:30:00 GMT+0000

0
On

It is epoh time format.

Somebody already answered about that in https://stackoverflow.com/a/20732668/379779 and test your epoh time in this website http://www.epochconverter.com/

3
On

Here is one solution that will provide the date correctly formatted.

String d = "531772200000";

SimpleDateFormat newFormatter = new SimpleDateFormat("dd-MMM-yyyy");

try {
    Date date1 = new Date(Long.parseLong(d));
    System.out.println(newFormatter.format(date1)); //Will print out as 07-Nov-1986
} catch (ParseException e) {
    e.printStackTrace();
}

Another solution is to use Joda Time with a solution below.

String d = "531772200000";
DateTime newDate = new DateTime(Long.parseLong(d));
DateTimeFormatter dd = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime date = dd.parseDateTime(newDate.toString());

System.out.println(date.toString("dd-MMM-yyyy")); //Prints out as 07-Nov-1986

Personally I prefer to use the second solution (Joda Time) as it is much nicer and easier.