evaluating localDate.now().minusDays(integer)

715 views Asked by At

I've been working on a program that stores a series of scheduled events (home made ServiceRequest objects) in an xml file (f), and when I want to work with them I call a readData() method (see below) which loads the ServiceRequest's into serviceQueue(an ArrayList<ServiceRequest>) and I tag a null object on at the end of the file to mark the end of the ServiceRequest objects.

As I'm loading the objects in the array, I've been checking the event's date (LocalDate object) to remove events more than 270 days old. Now here's my issue: immediately after changing the integer 270 to 180 to try and track less data, the majority of events got deleted(i.e. current events that shouldn't have been, like all of today's etc...). It was literally the only thing I changed, and as soon as I switched back to 270 and recovered the events from a backup, everything went back to normal. 210 also seemed to cause my evaluation to fail as well.

Not sure what I'm missing, maybe something about how LocalDate.now().minusDays(XXX) would be evaluated? I can include more of my code if needed, but this was the only thing I changed and the method is pretty basic:

public static void readData() {
    serviceQueue.clear();
    try {
        Boolean complete = false;
        FileReader reader = new FileReader(f);
        ObjectInputStream in = xstream.createObjectInputStream(reader);
        while(complete != true) {   
            ServiceRequest test = (ServiceRequest)in.readObject();      
            if(test != null && test.getDate().isAfter(LocalDate.now().minusDays(210))) { 
                serviceQueue.add(test); }
                else { complete = true; }
        }
        in.close(); } 
    catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } 
}

I use LocalDateTime variables start and stop to track the events date/time so the .getDate() method simply returns start in a LocalDate format:

    public LocalDate getDate() { return start.toLocalDate(); }
1

There are 1 answers

1
Dawid Wysakowicz On BEST ANSWER

The datÄ™ checking is ok. The problem is setting complete to true in case the date does not match.

if(test != null &&test.getDate().isAfter(LocalDate.now().minusDays(210))) { 
            serviceQueue.add(test); }
            else { complete = true; }

Try the code below:

if(test != null) { 
     if (test.getDate().isAfter(LocalDate.now().minusDays(210)) {
        serviceQueue.add(test);
     }
}
else { 
  complete = true; 
}