0

alright, so what I have is a ScheduledExxecutorService which checks every second if the date from the list of dates has passed, code:

public void dateCheckinator(){
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Calendar cal = Calendar.getInstance();
                for(int i=0;i<dateList.size();i++){
                    if (cal.getTime()>dateList.get(i)){
                        kaladeList.add(kaladeList.get(i));
                    kaladeListats.add(kaladeListats.get(i));
                    }
                }
                System.out.println(cal.getTime());
            }
        };
        scheduler.scheduleAtFixedRate(r, 1, 1, TimeUnit.SECONDS);
    }

However I get error at if statement, how do I check if the date has passed ? I will change scheduler to checking every minute or so instead of seconds later on.

Granitas
  • 302
  • 1
  • 4
  • 11
  • FYI: this code seems really flawed `kaladeList.add(kaladeList.get(i));`. Why would you add an element to `kaladeList` that you are getting from `kaladeList`? Shouldn't this be `kaladeList.add(dateList.get(i));` – John B Oct 23 '12 at 15:04
  • I appreciate the concern, but that's exactly what I want to do, copy the earlier members of the list to the end of the list. I'm sure there is a more efficient way of doing that but I'm still learning and just experimenting with this whole program. – Granitas Oct 23 '12 at 15:39
  • dateList type is Date and I got an unspecified error. works fine now with the suggestion posted below ! – Granitas Oct 23 '12 at 15:42
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). `Instant.now().isAfter( myDate.toInstant() )` – Basil Bourque Feb 23 '18 at 00:25

1 Answers1

7

In Java you are supposed to use after or before Methods.

if (cal.getTime().after(dateList.get(i))){
   kaladeList.add(kaladeList.get(i));
   kaladeListats.add(kaladeListats.get(i));
}
Robin
  • 3,512
  • 10
  • 39
  • 73