-1

I have a string type date. I want to change it firstly into date datatype. But it is not formating date properly as I want.Here is my code

String a="12/11/2010";

        Date d=new Date();
        SimpleDateFormat as=new SimpleDateFormat("dd/MM/yy");
        d=as.parse(a);
  System.out.println(a);

Output is like this :Sun Jan 01 00:00:00 IST 2012

But I want output like this 12-10-2014 in Date data type, not in String data type.

Grice
  • 1,345
  • 11
  • 24
user3493817
  • 103
  • 1
  • 2
  • 6
  • possible duplicate of [What is the default timezone in java.util.Date](http://stackoverflow.com/questions/11337557/what-is-the-default-timezone-in-java-util-date) – Basil Bourque Oct 20 '14 at 18:01

2 Answers2

1
System.out.println(as.format(d));

You should use your dateformatter object to format your date d.

If you want to parse it as 12/11/2010 and output as 12-11-2010 you could have 2 different dateformatters one for parse and one for output. Ex.

    String a="12/11/2010";

    Date d=new Date();
    SimpleDateFormat as=new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat as1=new SimpleDateFormat("dd-MM-yyyy");
    d = as.parse(a);
    System.out.println(as1.format(d));
brso05
  • 13,142
  • 2
  • 21
  • 40
0

A Date is an instant in time, and although it has a toString() that is not replaceable. Instead you use a DateFormat (like the one you have) to format the output

String a="12/11/2010";
SimpleDateFormat as=new SimpleDateFormat("dd/MM/yy");
Date d=as.parse(a);
System.out.println(as.format(d));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249