I have a string in the form "2013-09-18". I want to convert it into a java.util.Date. I am doing this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(currentDateText);
The convertedCurrentDate is coming as 'Wed Sep 18 00:00:00 IST 2013'
I want my output in the form '2013-09-18'
Hour, minutes and seconds shouldnt come and it should be in the form yyyy-MM-dd.
Using the Parse API With a Custom Formatter. Converting a String with a custom date format into a Date object is a widespread operation in Java. For this purpose we'll use the DateTimeFormatter class, which provides numerous predefined formatters, and allows us to define a formatter.
You may need to format
the out put as follows.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse("2013-09-18");
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
Use
String convertedCurrentDate =sdf.format(sdf.parse("2013-09-18"));
Output:
2013-09-18
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With