0

I want format a date given in the following format 1st March 1990. The date should be formatted to YYYY-MM-DD. I have the following code. It gives me an unparsable date. From this i can understand, this is not the correct way to format this date as its not a valid pattern.

public class DateFormattingTest {
    public static void main(String[] args) throws ParseException {
        String dateString = "1st March 1984";

        dateString = dateString.replaceFirst("[a-zA-Z]{2}","") ;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d MMMM yyyy");
        Date rightNow = simpleDateFormat.parse(dateString);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = dateFormat.format(rightNow);
        System.out.println(formattedDate);
    }
}

I have revised and looked for date formatting patterns as well. I cannot find something related to this pattern "1st March 1990". I don't want sample code for this issue. I want to find out what am i doing wrong in here? Can someone suggest an approach to parse such a date?

Thanks.

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
benz
  • 4,561
  • 7
  • 37
  • 68
  • Guys, i have added the code and updated my question subject also, if anyone stucks at the same problem. – benz Oct 10 '14 at 20:06

3 Answers3

3

You have three problems.

  • First, you're trying to parse the date using the format YYYY-MM-DD. That's not the format of your data, which is why parsing is failing.
  • Second, you're expecting a Date object to retain information about a particular format. It doesn't. Instead, you would parse from one text format to a Date, and then use another DateFormat (with the desired output format) to format the Date into a String. Date.toString() will always use the same format, regardless of how you arrived at the Date.
  • Third, your format of YYYY-MM-DD isn't really what you want - you want yyyy-MM-dd. YYYY is the "weekyear", and DD is the "day of year".

I don't know of any SimpleDateFormat approach which would handle the ordinal part of your input string ("1st", "2nd" etc) - you'll probably need to put a bit of work into stripping that out. Once you've got a value such as "1 March 1990" you can parse with a SimpleDateFormat using the pattern d MMMM yyyy. Make sure you set the time zone and the locale appropriately.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Since the pattern is characterized by the ordinal markers being the first letters in the string, I think they can be easily removed with `dateString.replaceFirst( "[a-zA-Z]{2}", "" )` – RealSkeptic Oct 10 '14 at 19:54
  • 1
    @RealSkeptic: Yes, if the value is assumed to be correct to start with, that should work.] – Jon Skeet Oct 10 '14 at 19:56
  • @RealSkeptic, i am almost there, lemme check with this regular expression replacement. This site is just awesome. – benz Oct 10 '14 at 19:59
  • @JonSkeet what do you mean in point-2 when we say Date.toString() will always use the same format? Which format? – benz Oct 10 '14 at 20:06
  • 1
    @benz: The one specified in the [Javadoc for `Date.toString()`](http://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toString--). When in doubt, check the docs :) – Jon Skeet Oct 10 '14 at 20:07
  • Thanks a ton Jon. :) Sorry i will next time first reference the javadoc. Thanks a ton again. – benz Oct 10 '14 at 20:09
2

Your date 1st was not incorporated into the Java DateFormat. If you can switch to 1 and use the appropriate DateFormat, the parsing of Date will work or else you would need to convert the ordinal number to number by stripping the suffix. This might be a good related post to peek at.

Community
  • 1
  • 1
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
  • Piyush, what do you exactly mean by converting to Ordinal number? There is not format in java, taking care of ordinal numbers. – benz Oct 10 '14 at 19:56
  • @benz: I mean the other way around, converting the ordinal number to number. Have edited my post. – Piyush Mattoo Oct 10 '14 at 20:02
0

The problem is that your dateString does not match the pattern specified in your simpleDateFormat format. It is expecting a date in the format YYYY-MM-DD, the meaning of these symbols can be found here.

PeterK
  • 1,697
  • 10
  • 20