Tuesday, October 23, 2018

java - Convert String date of format yyyy-MM-dd HH:mm:ss.SSS to String MM/dd/yyyy




I am trying to convert String date of format "yyyy-MM-dd HH:mm:ss.SSS" to String "MM/dd/yyyy"



My code goes like this:



SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String dateInString = "2015-07-16 17:07:21";
try {
Date date = formatter.parse(dateInString);

System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}


I am getting error:




java.text.ParseException: Unparseable date: "2015-07-16 17:07:21" at

java.base/java.text.DateFormat.parse(DateFormat.java:395) at
MyClass.main(MyClass.java:10)




Please let me know how should I fix this. I know this might be a duplicate but i didn't find any luck. Thanks.


Answer



Try this



String dateInString = "2015-07-16 17:07:21"
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-ss HH:mm:ss");

SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
try {
Date date = inputFormat.parse(dateInString);
System.out.println("Date ->" + outputFormat.format(date));
} catch (ParseException e) {
e.printStackTrace();
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...