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?
You can also use substring()
If you want a space in front of the date, use
You can just use:
It works perfectly!
You could try java 8 new
date
, more information can be found on the oracle documentation.Or you can try the old one