Friday, 8 April 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...