Ali Gajani
Ali Gajani

Reputation: 15091

Unable to format my Date into the right format?

My date is of the format: mon/jan/13/20/01/56/0000/2014

Exception in thread "main" java.text.ParseException: Unparseable date: "mon/jan/13/20/01/56/0000/2014"

//fix the date and time
SimpleDateFormat sdf = new SimpleDateFormat("EEE/MMM/dd/kk/mm/ss/zzzz/yyyy");
Date d = sdf.parse(tweet[0].replaceAll(" ","/"));   
System.out.println(new SimpleDateFormat("MM/dd/yyyy kk:mm:ss").format(d));

I want it to be in this format: MM/DD/YY KK:MM:SS

I have browsed lots of SO posts but unable to find anything useful.

Infact, the piece of code that I have obtained aforementioned is from a SO post.

However, I am getting the error.

Upvotes: 1

Views: 104

Answers (1)

Artur Malinowski
Artur Malinowski

Reputation: 1144

Your TimeZone is in incorrect format, see docs. Try with mon/jan/13/20/01/56/+0000/2014.

Edit: I supposed you can fix it with the hint above; anyway - not very clean code, but should work:

SimpleDateFormat sdf = new SimpleDateFormat("EEE/MMM/dd/kk/mm/ss/zzzz/yyyy");
String date = tweet[0].replaceAll(" ","/");
if (date.split("/")[6].length() == 4) {
    date = new StringBuilder(date).insert(20, "+").toString();
}
Date d = sdf.parse(date);
System.out.println(new SimpleDateFormat("MM/dd/yyyy kk:mm:ss").format(d));

Upvotes: 3

Related Questions