-5

I am working on dateFormat in java,I am facing some problem,Please check my code below

String sDate = "2014-05-20";
Date convertDate = new Date(s);
System.out.println(convertDate );

It's working fine but the output is like

OutPut:Tue May 20 13:52:40 IST 2014

but I dont' won't the output like this ,I want out put same as String format

ExpectedOutput :2014-05-20

how can i do this any one help me

user2963481
  • 1,863
  • 5
  • 17
  • 20
  • Why not use [LocalDate](http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) class of Java 8? You're getting the output you're getting because the `toString()` method of `Date` class returns the kind of `String` that is getting displayed. – Aman Agnihotri May 21 '14 at 07:01
  • http://stackoverflow.com/questions/23394809/removing-time-from-my-date-variable/23394829#23394829 – jmj May 21 '14 at 07:02
  • Try researching first before posting – coder hacker May 21 '14 at 07:05

3 Answers3

4

Use SimpleDateFormat for example:

SimpleDateFormat sdf = new SimpleDateFormat(yyy-MM-dd);
Date yourNewDate = sdf.parse(sDate);
RMachnik
  • 3,444
  • 1
  • 29
  • 47
1

try this

        SimpleDateFormat d=new SimpleDateFormat("yyyy-MM-dd");
        Date d1=d.parse("2014-05-20");
        System.out.println(d.format(d1));

working example

SpringLearner
  • 13,195
  • 20
  • 69
  • 111
0

You can use SimpleDateFormat to set the Format of the Date. Do like below:

String sDate = "2014-05-20";

SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
Date convertDate =dateFormat.parse(sDate);
System.out.println(convertDate );
Gundamaiah
  • 719
  • 2
  • 6
  • 29