1

I have some different format of date to parse but I cannot recognize them with SimpleDateFormat. Can anybody help me to find patterns for this dates:

  • 6 July 1892
  • 9 May 1915
  • February 335
  • 1768-02-12

and

  • 23 september 63 bc
  • 19 august ad 14

Thanks

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
DX89B
  • 1,593
  • 2
  • 11
  • 11
  • 1
    http://stackoverflow.com/q/3850784/1140748 – alain.janinm May 27 '12 at 14:40
  • 1
    first time I have come across a question which speaks about converting time specified in BC :) +1 – mprabhat May 27 '12 at 14:41
  • Well none of them are simpledateformat 6 July 1892 would be something like "d MMMM yyyy". You are gong to have some fun with the BC one and is 1768-02-12 12 of Feb or 2nd Dec – Tony Hopkinson May 27 '12 at 14:42
  • Also note that some are just impossible to parse reliably. 1768-02-12 could mean february 12 or december 2. – JB Nizet May 27 '12 at 14:43
  • check [this](http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm) i hope it will help you. – Simon Dorociak May 27 '12 at 14:45
  • 1
    The US really should "get with the program": Everywhere else uses "sane" `yyyy-MM-dd` or `dd-MM-yyyy` (note how it goes from biggest to smallest or visa versa). American format is like writing "five hundred forty three" as 534! – Bohemian May 27 '12 at 14:46
  • @Bohemian that doesn't hurt at all when I hear about miles yards pounds and ounces :) – nawfal Jan 30 '14 at 08:43

4 Answers4

1

Joda is generally better if you have to parse using multiple formats. For example,

        private static DateTimeFormatter dateFormatter;
        String[] validDateFormats = new String[] { "dd-MMM-yyyy HH:mm:ss",
                "yyyy-MM-dd HH:mm:ss.SSS" };
        DateTimeParser[] parsers = new DateTimeParser[validDateFormats.length];
        for (int i = 0; i < validDateFormats.length; ++i) {
            parsers[i] = DateTimeFormat.forPattern(validDateFormats[i])
                    .getParser();
        }
        dateFormatter = new DateTimeFormatterBuilder().append(null, parsers)
                .toFormatter().withZone(DateTimeZone.UTC);

Now this dateFormatter will parse correctly if the input matches any of the formats:

        inputDate = dateFormatter.parseDateTime(dateStr);
        DateTimeFormatter outputFormat = DateTimeFormat
                .forPattern("yyyy/MM/dd");
        String outputString = inputDate.toString(outputFormat);

The format strings can be looked up from here: http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormat.html

Hari Menon
  • 33,649
  • 14
  • 85
  • 108
1

You just provide possibilities of patterns you want your dates to be parsed by and run through them finding the first matching.

Run solution.

d1e
  • 6,372
  • 2
  • 28
  • 41
  • your is the most simpler and effective solution :) my problem is that i doesn't use the locale .. so it was formatted in italian i think :) – DX89B May 27 '12 at 16:06
  • 1
    Common problem for non-UK/US citizens, happy to help. – d1e May 27 '12 at 16:16
0

Here you can find a great generator for creating every date format you can imagine http://www.fileformat.info/tip/java/simpledateformat.htm

E.g. your first both examples are generated with dd MMMM yyyy ;)

nullpointr
  • 524
  • 4
  • 18
  • 1
    this doesnt answer how to do this in Java – mprabhat May 27 '12 at 14:40
  • you just need to put the generated format into SimpleDateFormat() – nullpointr May 27 '12 at 14:42
  • follow the link on the site (http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html). there's a description how to convert in bc. "...During parsing, only strings consisting of exactly two digits, as defined by Character.isDigit(char),will be parsed into the default century. Any other numeric string, such as a one digit string, a three or more digit string,or a two digit string that isn't all digits (for example, "-1"), is interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC." – nullpointr May 27 '12 at 14:54
0

Try these

27-May-2012:

System.out.println(new SimpleDateFormat("d-MMM-YYYY").format(new Date()));

27-05-2012 :

System.out.println(new SimpleDateFormat("d-MM-YYYY").format(new Date()));

27-05-12 :

System.out.println(new SimpleDateFormat("dd-MM-YY").format(new Date()));
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75