0

I have a particular date format.I can subtract 2 dates but I can't achieve to subtract today's date from a future one.

I found the way to get today's date

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

Here is the way I subtract 2 dates(Strings)

public boolean DateDiff(String datep,String dater) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate date1 = LocalDate.parse(datep, formatter);
        LocalDate date2 = LocalDate.parse(dater, formatter);
        
        long elapsedDays = ChronoUnit.DAYS.between(date1, date2);

I can't turn the current day to a String so I can have 2 same types of variables to subtract.What kind of type is the LocalDateTime now

8
  • 1
    "I can't turn current day to a String": but you already did this - dtf.format(now). You don't need to, of course, just compare the future date to LocalDate.now().
    – James_D
    Aug 27, 2020 at 18:18
  • Check this out: stackoverflow.com/questions/22326339/…. This should clear all of your doubts. Aug 27, 2020 at 18:29
  • Does this answer your question? How create Date Object with values in java Aug 27, 2020 at 22:05
  • I tried Frank's way. but it gives me the Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: text error.Pretty odd.I've succeded subtracting two dates(strings) by inputs in textfields and now that I try to subtract today's day from a future one it gives me this error
    – Dean
    Aug 27, 2020 at 22:36
  • I agree it’s pretty odd. There’s nothing in the code in @FrankMi’s answer that could give you a NullPointerException (I ran each of the four snippets without getting any exception).
    – Ole V.V.
    Aug 28, 2020 at 4:45

4 Answers 4

3

To subtract dates is as easy as this one using the minusDays() function of LocalDate:

LocalDate now = LocalDate.now();
LocalDate thirtyDaysAgo = now.minusDays(30);

If you have a standard string of date, see "2020-08-27", you can parse it this way:

String date = "2020-08-27";
LocalDate thirtyDaysAgo = LocalDate.parse(date).minusDays(30);
System.out.println(thirtyDaysAgo); //prints 2020-07-28

If you have a special format of date, see "2020/08/27", you parse it this way:

String date = "2020/08/27";
LocalDate thirtyDaysAgo = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd")).minusDays(30);
System.out.println(thirtyDaysAgo); //prints 2020-07-28

To turn a LocalDate to a String with format, you do it this way:

LocalDate date = LocalDate.now();
String formatedDate = date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println(formatedDate); //prints 2020/08/27
1

Use LocalDate instead of LocalDateTime to get today's date

LocalDate currentDate = LocalDate.now();

For some reason if you still want to use LocalDateTime to get today's date, this is how you can do it

LocalDateTime currentDateTime = LocalDateTime.now();
LocalDate currentDate = currentDateTime.toLocalDate();

if you want to convert currentDate to String in your desired format as follows:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");   
String currentDateString = currentDate.format(formatter);

if both the dates you have are of type LocalDate, then you can find the number of days in between them as follows:

public long DateDiff(LocalDate date1,LocalDate date2) {

    return Duration.between(date1, date2).toDays();
}

if both the dates you have are of type String, then you can find the number of days in between them as follows:

public long DateDiff(String dateString1,String dateString2) {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate date1 = LocalDate.parse(dateString1, formatter);
    LocalDate date2 = LocalDate.parse(dateString2, formatter);

   return Duration.between(date1, date2).toDays();
}

Hope that solves your problem

0

You can use joda-time library its a bliss when working with dates in java.

Subtract 2 dates

DateTime yesterday = new DateTime().withTimeAtStartOfDay().minusMinutes(10);

DateTime twoDaysFromToday = new DateTime().withTimeAtStartOfDay().plusDays(2);

Days.daysBetween(yesterday.toLocalDate(), twoDaysFromToday.toLocalDate()).getDays();
1
  • When the questioner is already using java.time, the modern Java date and time API, suggesting Joda-Time (a good library in its days) seems to be a step backward?
    – Ole V.V.
    Aug 27, 2020 at 18:39
0

Use LocalDate (not LocalDateTime).

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    
    LocalDate today = LocalDate.now(ZoneId.of("Asia/Calcutta"));
    LocalDate futureDate = LocalDate.parse("26/09/2020", formatter);
    long elapsedDays = ChronoUnit.DAYS.between(today, futureDate);
    
    System.out.println("Days until future date: " + elapsedDays);

Output was when running just now:

Days until future date: 29

Not the answer you're looking for? Browse other questions tagged or ask your own question.