Wednesday, 21 November 2018

Change date format in a Java string

I've a String representing a date.



String date_s = "2011-01-18 00:00:00.0";



I'd like to convert it to a Date and output it in YYYY-MM-DD format.




2011-01-18




How can I achieve this?







Okay, based on the answers I retrieved below, here's something I've tried:



String date_s = " 2011-01-18 00:00:00.0"; 
SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
System.out.println(dt1.format(date));



But it outputs 02011-00-1 instead of the desired 2011-01-18. What am I doing wrong?

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 ...