Amal Madawa Amal Madawa - 3 months ago 25
Java Question

how to Parse String Date to MongoDB date type

i'm have string dates like

Wed Aug 17 17:22:51 IST 2016
to parse to ISOdate. so I have tried following code to do that.

String tdate = "Wed Aug 17 17:22:51 IST 2016";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date pubDate = sdf.parse(tdate);


but it gave me:

java.text.ParseException: Unparseable date: "Wed Aug 17 17:22:51 IST 2016"

Answer

try parsing with

SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");

hope it helped