-1

The question is not a duplicate, do not confuse it. It addresses mainly how you cannot store the variable of type String in Date, but you can format it after casting it. The other answer marked as "duplicate" is vage and not related...

I'm trying to convert a String X = ""01021990"; to a data type Date, but everytime I store it, it gives me an error that cannot be cast

This is what works:

    String date = "01062014";
    DateFormat dateInput = new SimpleDateFormat("MMddyyyy");
    DateFormat desireDate = new SimpleDateFormat("MM/dd/yyyy");


    System.out.println(desireDate.format((Date)dateInput.parse(date)));

This is what I'm trying to do that doesn't work:

DateFormat inputDate = new SimpleDateFormat("MMddyyyy");
DateFormat desireDateFormat = new SimpleDateFormat("MM/dd/yyyy");

Date xs = desireDateFormat.format((Date)inputDate.parse(date));

However if I just print it, it does works:

System.out.println(desireDateFormat.format((Date)inputDate.parse(date)));

But I'm trying to store it into the data type Date so I can pass it to a constructor.

Thank you

Arturo
  • 3,254
  • 2
  • 22
  • 61
  • 1
    Is there a specific reason you are using the old and cumbersome date API instead of the nice, new, modern one from `java.time`? – Zabuzard Jul 03 '19 at 19:18
  • Possible duplicate of [How can I change the date format in Java?](https://stackoverflow.com/questions/3469507/how-can-i-change-the-date-format-in-java) – Tom Jul 03 '19 at 19:33
  • Your excuse why it is not a duplicate is very funny when I see you accepted the answer below telling you the exact same as the answer in the linked question. Do you understand what the "duplicate" feature on Stack Overflow even means? – Tom Jul 03 '19 at 19:43

1 Answers1

2
desireDateFormat.format((Date)inputDate.parse(date)) 

returns String. Of course, you can println that but cannot cast String to Date This would work: Date xs = inputDate.parse(date);

Spasoje Petronijević
  • 1,476
  • 3
  • 13
  • 27
  • What I'm trying to do is store it into a Date variable, isn't that possible? – Arturo Jul 03 '19 at 19:17
  • That would it give me this format: `Mon Jan 06 00:00:00 EST 2014` and I want it like this: `MM/dd/yyyy` – Arturo Jul 03 '19 at 19:20
  • You have your date now in xs variable. You can print it in your format later with desireDateFormat.format(xs) – Spasoje Petronijević Jul 03 '19 at 19:24
  • What if I want to store it in a file? or pass it to a constructor? – Arturo Jul 03 '19 at 19:28
  • If you want to pass your date to constructor you can use xs variable, its Date object. Date object don't care about format, you can later again use DateFormat to print it in way you want. – Spasoje Petronijević Jul 03 '19 at 19:34
  • @Arturo If you store it in a file, you probably want to format it. If you pass it to a constructor, it's up to how you write the constructor. It's usually better to use the Date object than a formatted date string in most cases, since you can perform any necessary date/time manipulation without having to re-parse the date. It makes sense to only format the date into a string in situations where you *need* it to be a string. – Jordan Jul 03 '19 at 19:36
  • Thank you for your help! – Arturo Jul 03 '19 at 19:37