0

I am trying to specify a function for parsing a date in String form to a Date.

String startDate = "14-08-2014";
String endDate = "21/08/2020";
String middleDate = "Sep 27, 2018";

SimpleDateFormat sdf[] = new SimpleDateFormat[] {new SimpleDateFormat("dd/MM/yyyy"), new SimpleDateFormat("dd-MM-yyyy"), new SimpleDateFormat("MMM dd, yyyy")  };

Date date = null;
for (DateFormat formatter : sdf) {
    try {
        date = formatter.parse(middleDate);
        break;
    } catch (ParseException e) {
}

System.out.println(date);

It works for two formats except the last one. If it uses middleDate it finishes with Date equal to null. Can anybody tell me what is wrong?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Michal
  • 610
  • 10
  • 24
  • 3
    `SimpleDateFormat` is old hat. You should be using the `java.time` package – Michael Oct 11 '18 at 13:36
  • Maybe you can check this answer: https://stackoverflow.com/a/33428847/6882497 – Akiner Alkan Oct 11 '18 at 13:38
  • you should log/print exception in the catch block – Derrick Oct 11 '18 at 13:42
  • 1
    I suspect *this* code doesn't work because of the default system locale. But I'd strongly agree with Michael that using `java.time` is the way forward here. – Jon Skeet Oct 11 '18 at 13:47
  • @Ivar I tried it in IntelliJ and it prints null. No clue what is inside of the engine you used.... – Michal Oct 11 '18 at 13:59
  • No repo. Voting to close. – Roshana Pitigala Oct 11 '18 at 14:02
  • 1
    @Michal It could very well be what Jon Skeet said. Try to add `Locale.ENGLISH` as a second parameter to your `new SimpleDateFormat()`. – Ivar Oct 11 '18 at 14:05
  • 1
    Correct. Thanks. I use the following code and it works: SimpleDateFormat sds[] = new SimpleDateFormat[]{ new SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH), new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH), new SimpleDateFormat("MMM dd, yyyy",Locale.ENGLISH)}; – Michal Oct 11 '18 at 14:13
  • @Michael Thanks for hint about java.time... Can you post it here? – Michal Oct 11 '18 at 14:16
  • @Ivar Yes it is. I would like to also handle dates for various countries. – Michal Oct 11 '18 at 14:18
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 11 '18 at 14:47
  • @Michal Search Stack Overflow thoroughly before posting. You can assume any basic date-time question has already been asked and answered. – Basil Bourque Oct 11 '18 at 18:35

2 Answers2

1

there is a solution using the java.time, but there still needs to be used LOCALE.ENGLISH. I do not know what would be the way to make it working without using LOCALE...

 String input = "Mar 23, 1994";
    try {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy").withLocale(Locale.ENGLISH);
        LocalDate date = LocalDate.parse(input, formatter);
        System.out.printf("%s%n", date);
    }
    catch (DateTimeParseException exc) {
        System.out.printf("%s is not parsable!%n", input);
        throw exc;      // Rethrow the exception.
    }
Michal
  • 610
  • 10
  • 24
-2

Your parser is right your month should be "Set"

"Set 27, 2018"


Jan Feb Mar Apr May Jun Jul Aug Set Oct Nov Dec
Gnk
  • 645
  • 4
  • 11
  • Set as in Setpember? :) – gherkin Oct 11 '18 at 13:45
  • 1
    That seems unusual to me. What makes you think that would work? It doesn't on my machine. It would work in a culture that used "Set" as abbreviated form of the localized name for September, but I don't know of any such cultures that have the "regular English" abbreviated names for all the other months. – Jon Skeet Oct 11 '18 at 13:46
  • I tested here before post – Gnk Oct 11 '18 at 13:52
  • 1
    And what's your system culture? As I say, that's very surprising. – Jon Skeet Oct 11 '18 at 14:10
  • Hint: `System.out.println(Locale.getDefault());` – LuCio Oct 11 '18 at 14:13
  • my culture is pt-BR... I google for abbreviations and I found this https://www.linguee.com/english-portuguese/translation/jan+feb+mar+apr+may+jun+jul.html. that is why I thought it was "SET" – Gnk Oct 11 '18 at 14:19
  • Okay, but at that point the lower line should be: `jan fev mar abr mai jun jul ago set out nov dez` - you've changed *just* the abbreviation for September, which is why it was so confusing. – Jon Skeet Oct 11 '18 at 14:37