-1

I am trying to ensure that user entered a correct date format 10/08/2015 for example.

try{
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
             d = sdf.parse(date.getText().toString());
        }catch(ParseException ex){
            Toast.makeText(MainActivity.this,
                    "Date format is wrong.", Toast.LENGTH_LONG).show();
            return;
        }

But it still parses most of the wrong inputs like 111/10/2013 or 11/109/2013 or some input containing characters.

Another question is how do i convert dd/mm/yyyy type date to NameofMonth day,year string ?

so 01/04/2018 will be converted to Apr 1,2018

dongerpep
  • 109
  • 1
  • 4
  • 11
  • What type is `date`? – Joe C Apr 07 '18 at 16:37
  • I believe the variable d is of type date. So, you can check this https://stackoverflow.com/questions/20231539/java-check-the-date-format-of-current-string-is-according-to-required-format-or – Vasco Lopes Apr 07 '18 at 16:38
  • 1
    d is a Date d, date is edittext – dongerpep Apr 07 '18 at 16:39
  • To print it out, you'd make a second SimpleDateFormat with the output format you want, and call sdf2.format(d) – Gabe Sechan Apr 07 '18 at 16:51
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Apr 07 '18 at 20:57

1 Answers1

1
    DateTimeFormatter enteredFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    DateTimeFormatter usDateFormatter = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.MEDIUM)
            .withLocale(Locale.US);
    LocalDate d;
    try {
        d = LocalDate.parse(date.getText(), enteredFormatter);
        System.out.println(d.format(usDateFormatter));
    } catch (DateTimeParseException dtpe) {
        Toast.makeText(MainActivity.this,
                "Date format is wrong.", Toast.LENGTH_LONG).show();
        return;
    }

Getting the incorrect dates from your question this code will catch a java.time.format.DateTimeParseException: Text '111/10/2013' could not be parsed at index 2 or java.time.format.DateTimeParseException: Text '11/109/2013' could not be parsed at index 5 and therefore give the error message to the user. Given a correct date string like 11/10/2013 it prints like:

Oct 11, 2013

A detail that I haven’t tested on Android: As I understand, EditText.getText will return an Editable, which implements CharSequence. Since LocalDate.parse accepts a CharSequence, you can pass date.getText() with no need for .toString().

When formatting a date for an audience, the first thing you should consider is using one of Java’s built-in formatters for that audience’s locale. Your requested format agrees with the MEDIUM format for US locale. I took this to be no coincidence. So do take advantage.

At the same time I am taking advantage of java.time, the modern Java date and time API. SimpleDateFormat is notoriously troublesome and long outmoded. I avoid it. The modern API is so much nicer to work with.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161