0

I need to set up a program that compares the date of creation of a date object(say date object 1), with another a future date or date object(say date object 2) that represents the deadline. For example, if i create an initial datetime object, I want to be able to compare the current date of creation with lets say, a date 18 days after said date(deadline).I don't want to hardcode the actual dates and deadlines. It should return a bool( true when current date is equal or later than the deadline date, false otherwise). Eventually, I'll want to store the date of the deadline in an external database, then when appropriate compare said deadline date with the current date.

I can get the current date with the code below but unsure how to obtain a deadline day by specifyinging "x" days from the current date instead of hardcoding the date values.

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
    LocalDateTime now = LocalDateTime.now();
    System.out.println(dtf.format(now));

I want to be able to return a boolean; false if current date is before deadline date, true if current date is >= deadline date

  • `LocalDate::isAfter` is your friend if you are using Java 8 or later. – Michał Krzywański Jun 18 '19 at 20:20
  • You want to compare a given date against a deadline for which you dont know how far away it is? – daniu Jun 18 '19 at 20:20
  • @daniu i know how far away is is, i just don't want to hardcode the date value since that would be inefficient when implementing multiple deadlines. –  Jun 18 '19 at 20:22
  • If you're always making the deadline date x days after the current date, then your check for whether or not the current date is after the deadline will always be false. Are you persisting the deadline in a database, and retrieving the object to compare the deadline against a *different* current date at some point in the future? – Jordan Jun 18 '19 at 20:26
  • @Jordan precisely! –  Jun 18 '19 at 20:27
  • Possible duplicate of [How to set a deadline in Java without hardcoding the values](https://stackoverflow.com/questions/56658688/how-to-set-a-deadline-in-java-without-hardcoding-the-values) – buræquete Jun 19 '19 at 01:32

0 Answers0