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

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Dean
  • 45
  • 1
  • 5
  • 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 '20 at 18:18
  • Check this out: https://stackoverflow.com/questions/22326339/how-create-date-object-with-values-in-java. This should clear all of your doubts. – Vikram Bhardwaj Aug 27 '20 at 18:29
  • Does this answer your question? [How create Date Object with values in java](https://stackoverflow.com/questions/22326339/how-create-date-object-with-values-in-java) – Brian Tompsett - 汤莱恩 Aug 27 '20 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 '20 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 '20 at 04:45
  • *What kind of type is the LocalDateTime now* The type is `LocalDateTime` and it’s a reference type. What did you mean by the question? It’s not compatible with `LocalDate` but can be converted into one as shown in the answer by Sikakollu. – Ole V.V. Aug 28 '20 at 05:06
  • @OleV.V. Ok so I tried the part of code outside the MouseEvent class and it works.I'm doing a Java FX Project.To make it short, if the elapsed days of the subtraction are more than 2, then I can delete a row from a table.Inside the if (event.getSource() == deleteBtn) statement I need to put another if statement to check the elapsed days to allow the deletion.Only when I insert the code in there it gives me the error – Dean Aug 28 '20 at 20:21
  • A wild guess, could it be that you are checking in the wrong order? It’s probably best to check whether the event source is the delete button before checking the dates. If that’s not enough. put in an explicit null check. Unsure whether it’s helpful, please check [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ole V.V. Aug 30 '20 at 17:25

4 Answers4

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
Frank Mi
  • 452
  • 3
  • 11
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

Sikakollu
  • 56
  • 6
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();
Ketan
  • 439
  • 4
  • 5
  • 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 '20 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

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