-1

I'd like to check if the system date (e.g. the current date) is before an expiry date before opening my software.

I have written the following code, however it throws a NumberFormatException. Why might this be happening?

public class dateController implements Initializable {

    @FXML Label lblDate;

    Alert alert = new Alert(AlertType.INFORMATION);

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();

        lblDate.setText(dateFormat.format(date));

        String a ="19/09/2018";

        String currentdate = lblDate.getText();

        String LastRunDate = currentdate;

        if(Integer.parseInt(currentdate) < Integer.parseInt(a)) {
            alert.setHeaderText(null);             
            alert.setContentText("Successfull");
            alert.show();
        }
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
XYZ
  • 15
  • 1
  • 6
  • 1
    Don't use `Integer.parseInt(..)`. Change the `String` date into a `LocalDate` then compare – SedJ601 Sep 17 '18 at 12:38
  • 2
    LocalDate is best way to compare dates in java – Jahongir Sabirov Sep 17 '18 at 14:32
  • I agree with Sedrick and Jahongir Sabirov: The classes you are using for dates: `Date`, `DateFormat` and `SimpleDateFormat`, are long outdated, and the last two in particular notoriously troublesome. Stay away from those. Use [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) instead. The mentioned `LocalDate` is from that API and exactly the right class to use. – Ole V.V. Sep 18 '18 at 16:19
  • Always search Stack Overflow before posting. You can assume any basic date-time question has already been asked and answered. – Basil Bourque Sep 18 '18 at 18:36

1 Answers1

1

Why might this be happening?

Integer.parseInt is fine for parsing a string like 42 into an int. You are trying to parse a string like 17/09/2018 using that method. Your string does not represent a valid int value (and frankly I don’t know which integer you had expected to come out of it?) Therefore the parseInt method throws the NumberFormatException.

Then what to do instead?

Use LocalDate for representing a date and its isBefore (or isAfter) to compare with another date.

    LocalDate expirationDate = LocalDate.of(2018, Month.SEPTEMBER, 19);
    LocalDate today = LocalDate.now(ZoneId.systemDefault());
    if (today.isBefore(expirationDate)) {
        alert.setHeaderText(null);             
        alert.setContentText("Successful");
        alert.show();
    }

Notice how much simpler it is compared to your code in the question. In this case you need no formatting or parsing. I am using the JVM’s time zone setting to get today’s date (which you also did in the question). So a user may possibly push the expiration by a couple of hours by changing time zone. If you want to avoid it, you can also hardcode the time zone as for example ZoneId.of("Asia/Ust-Nera")

If you do need to parse a string like 19/09/2018:

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    String a ="19/09/2018";
    LocalDate expirationDate = LocalDate.parse(a, dateFormatter);

    System.out.println("Expiration date is " + expirationDate);

This snippet outputs:

Expiration date is 2018-09-19

Stay away from Date, DateFormat and SimpleDateFormat. They are long outdated, and the last two in particular notoriously troublesome. Use java.time, the modern Java date and time API, instead.

Link: The Java™ Tutorials: Trail: Date Time

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