Question

I've got problem with SimpleDateFormat. I'm given string with date like this:

"2013-05-17 10:15:44"

And try to parse it with this:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formattedDate = simpleDateFormat.format(s2);

But the last line throws me an IllegalArgumentException. What is wrong with it? It just doesn't make any sense to me.

Was it helpful?

Solution

That's not how a DateFormat works: it is used to switch between Strings and Dates. So you have two basic operations:

Date date = format.parse(someString); //from String to date
String str = format.format(date); //from date to String

In your case, I suspect you wanted to do:

Date date = simpleDateFormat.parse(s2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top