Sunday, 10 September 2017

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...