Wednesday, November 21, 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

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