19

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.

lospejos
  • 1,976
  • 3
  • 19
  • 35
user2791546
  • 199
  • 1
  • 1
  • 3

5 Answers5

39

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
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Hi, How can i get java.util.Date in the (yyyy-MM-dd) format with return type of Date(java.util.Date)? – Sanshayan Jun 28 '16 at 08:57
  • 1
    You cannot. A `Date` does not and cannot have a format in it. – Ole V.V. Nov 24 '17 at 03:07
  • By default, parsing is lenient. Call `setLenient(false)` if you need to disable it - https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean) – Vadim Kotov Jan 31 '19 at 13:37
  • I know it is not related but just in case you want to convert it to java.sql.Date instead of java.util.Date, there is an easier way here at https://stackoverflow.com/a/18439059/1572286 – Youness Jul 16 '20 at 17:39
13

tl;dr

LocalDate.parse( "2013-09-18" )

… and …

myLocalDate.toString()  // Example: 2013-09-18

java.time

The Question and other Answers are out-of-date. The troublesome old legacy date-time classes are now supplanted by the java.time classes.

ISO 8601

Your input string happens to comply with standard ISO 8601 format, YYYY-MM-DD. The java.time classes use ISO 8601 formats by default when parsing and generating string representations of date-time values. So no need to specify a formatting pattern.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.parse( "2013-09-18" );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
6

You are creating a Date object, which is a representation of a certain point in the timeline. This means that it will have all the parts necessary to represent it correctly, including minutes and seconds and so on. Because you initialize it from a string containing only a part of the date, the missing data will be defaulted.

I assume you are then "printing" this Date object, but without actually specifying a format like you did when parsing it. Use the same SimpleDateFormat but call the reverse method, format(Date) as Holger suggested

Morfic
  • 15,178
  • 3
  • 51
  • 61
  • 1
    +1 this is the only correct answer to the question, since OP seems to be missing the fundamental understanding that a Date is a date, no matter how you print it. – adarshr Sep 18 '13 at 13:11
2
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String cunvertCurrentDate="06/09/2015";
Date date = new Date();
date = df.parse(cunvertCurrentDate);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
1

I convert String to Date in format ("yyyy-MM-dd") to save into Mysql data base .

 String date ="2016-05-01";
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 Date parsed = format.parse(date);
 java.sql.Date sql = new java.sql.Date(parsed.getTime());

sql it's my output in date format

mahdi kallel
  • 453
  • 1
  • 6
  • 15