achraf
achraf

Reputation: 69

Unable to parse string to the right date format

I have a date in string format and I want to parse it to date format "ddMMyy".

I used SimpleDateFormat as follows:

String stringDate = "050109";
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyy");
Date  date = dateFormat.parse(stringDate);

when I print the "date" I get in this format:

Mon Jan 05 00:08:00 CET 2009

even further when I marshal the Java object I get in new format:

2009-01-05T00:08:00+01:00

anyone have an idea how to get the right format?

Upvotes: 0

Views: 1115

Answers (2)

ivantod
ivantod

Reputation: 773

You need to format a Date object, parse method is for parsing strings into Dates, not the other way around. As you can see your string is being parsed correctly into a Date object. If you want to format this date for output at some point you can use the format method of SimpleDateFormat:

SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyy");
String formattedDate=dateFormat.format(date);

where date is your Date object.

Upvotes: 0

stacker
stacker

Reputation: 68992

If you want to print the date in the format you parsed:

String fmtDate = dateFormat.format( date );

Upvotes: 3

Related Questions