Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse String date in (yyyy-MM-dd) format

Tags:

java

date

parsing

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.

like image 528
user2791546 Avatar asked Sep 18 '13 13:09

user2791546


People also ask

Can we convert string to Date?

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.


1 Answers

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
like image 102
Ruchira Gayan Ranaweera Avatar answered Oct 01 '22 13:10

Ruchira Gayan Ranaweera