Converting string to readable SimpleDateFormat

162 views Asked by At

I am trying to add spaces to a string so that I can use it for SimpleDateFormat. I would need to add spaces to this:

String str = "Wed 3Jun15 03:22:15 pm"

I know how to put it into SimpleDateFormat as soon as I get it into this format:

String str = "Wed 3 Jun 15 03:22:15 pm"

Eventually I am only trying to pull d, HH:mm out of this date, so the rest can be trashed.

I have looked at this link: How to split a string between letters and digits (or between digits and letters)? and while it is closed, it doesn't work for what I am trying to do.

1

There are 1 answers

4
assylias On BEST ANSWER

This pattern should match your string:

EEE dMMMyy hh:mm:ss a

With your example:

String str = "Wed 3Jun15 03:22:15 pm";
SimpleDateFormat fmt = new SimpleDateFormat("EEE ddMMMyy hh:mm:ss a");
Date d = fmt.parse(str);
SimpleDateFormat output = new SimpleDateFormat("d, HH:mm");
System.out.println(output.format(d)); //prints 3, 15:22