-4

how to convert string date to java.util.date in java .

String date="12-12-2014 10:00:00"

Date d=2014-12-12 10:00:00;

Need this string date in date formate

gprathour
  • 14,813
  • 5
  • 66
  • 90
Akhila k.p
  • 31
  • 1
  • 3

2 Answers2

0

Try following code:

String test="12-12-2014 10:00:00";
Date date=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse(test);
String OutputDate=new SimpleDateFormat("yyyy-dd-MM HH:mm:ss").format(date);
System.out.println(OutputDate);

Output :

2014-12-12 10:00:00

The dig over here is to parse a string with date attributes using SimpleDateFormat into java.util.Date.

For more on SimpleDateFormat visit this link.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
0

Try this:

String date = "12-12-2014 10:00:00";
DateFormat inputDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
DateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(outputDateFormat.format(inputDateFormat.parse(date)));

Output:
2014-12-12 10:00:00

Basically you need to parse your date in the format provided and from one format (inputDateFormat) you need to convert it to the other format (done by outputDateFormat).

SMA
  • 36,381
  • 8
  • 49
  • 73
  • Its working ,But the out put is string type.I need as java.util.date type – Akhila k.p Dec 24 '14 at 12:10
  • If you want date output then you could just say `Date date = inputDateFormat.parse(date);` we cant change toString() of Date (i.e. String format of Date object) unfortunately to print it as per what you need hence we used formatter and its going to return us String object. – SMA Dec 24 '14 at 12:15
  • I tried this but ist giving output as Fri Dec 12 10:00:00 IST 2014 .need as 2014-12-12 10:00:00 in date formate – Akhila k.p Dec 24 '14 at 12:22