Wednesday 6 December 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.


itemprop="text">
class="normal">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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...