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