2

I want to put in a constructor an argument of type Date, so I thought that the best way to do that is to write a method that transforms a string into a date. Which is the easiest way to check if my string is a valid date?

This is my constructor:

Product product = new Product("Produs 1", 312.33, updateDate("23/12/2018"));

And this is the method that transforms the String into a Date.

public static Date updateDate(String date) {
    Date newDate = new Date();
    try {
        DateFormat dtF = new SimpleDateFormat("dd/MM/yyyy");
        newDate = dtF.parse(date);
        return newDate;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return newDate;
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
John R.
  • 420
  • 4
  • 14
  • 5
    You can parse it like that, but *don't return the current date when it is invalid*: throw an exception, or return an `Optional`. Otherwise, how do you tell the difference between "you gave me an invalid date" and "you gave me the current date"? – Andy Turner Jun 18 '18 at 20:00
  • 2
    If you're expecting invalid dates, consider returning `Optional`. Also, `updateDate()` is a confusing name for a date parsing method. – shmosel Jun 18 '18 at 20:02
  • 2
    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. Jun 18 '18 at 21:46

1 Answers1

-1

I would keep the try-catch block as small as possible. Also, in case there is a problem parsing the string, I would return Optional<Date> instead of the newly created date object.

public static Optional<Date> getDateFromString(String s) {
    Date date = null;
    DateFormat dtF = new SimpleDateFormat("dd/MM/yyyy");
    try {
        date = dtF.parse(s);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return Optional.ofNullable(date);
}

A good practice is to name the function with a more self-descriptive name like getDateFromString.

BrainArchitect
  • 558
  • 8
  • 16
  • 1
    This doesn't indicate to the caller that it failed though, because the optional still has a value. – Evan Knowles Jun 18 '18 at 20:51
  • 1
    Hello and thank you for your answer! If I return Optional.ofNullable(date); I get the error: "Error:(23, 35) java: incompatible types: no instance(s) of type variable(s) T exist so that java.util.Optional conforms to java.util.Date" – John R. Jun 18 '18 at 20:53
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jun 18 '18 at 21:55
  • 1
    Shouldn't that method signature say `Optional< Date >` rather than `Date`? – Basil Bourque Jun 18 '18 at 21:56
  • 1
    There are multiple bugs in this code. – Ole V.V. Jun 19 '18 at 03:56
  • @BasilBourque yes I fixed that now! Thanks! :) – BrainArchitect Jun 20 '18 at 12:19