DateTime compareTo does not apply to DateTime

The investigation and details for the accepted answer are given in the comments on the question.

I have a small Java project that reads a schedule and places Joda-Time intervals in a sorted map (in this case, TreeMap) for further processing. This is done through the ScheduledExecutorService:

executor.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            try {
                doWork();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }, 1, 1, TimeUnit.SECONDS);

doWork() then it reads the file, creates several intervals, and then fills the map using this comparator (specified in the map constructor):

@Override
public int compare(Interval o1, Interval o2) {
  return o1.getStart().compareTo(o2.getStart());
}

Then the code breaks in the comparator when inserting the very first interval. Usually I think that something is wrong with the interval itself, however I checked a few possibilities and noticed some strange things that I lost in:

  • The interval is fine, o1and o2DateTimes with the same long timestamp are valid.
  • . .
  • Eclipse, . . , .jar , .
  • try {
      doWork();
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
    

    try {
      doWork();
    } catch (Throwable e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
    

    . ( I.e. , ).

, JIT JVM, . , , , , :

  • Eclipse Java 7 (Eclipse 7.0.51, Build server: 7.0.25, 7.0.51 JRE).
  • Joda Eclipse, lib (2.1)
  • , .
  • Eclipse Ivy . - Eclipse, jar Java.

- : Method "compareTo" with signature "(Lorg/joda/time/ReadableInstant;)I" is not applicable on this object class org.joda.time.DateTime, .

, , .

:

private void doWork() {
        SortedMap<Interval, String> map = new TreeMap<>(new Comparator<Interval>() {
            @Override
            public int compare(Interval o1, Interval o2) {
                return o1.getStart().compareTo(o2.getStart());
            }
        });

        Collection<String> col1 = new HashSet<>();
        Collection<String> col2 = new HashSet<>();
        String string = "";
        long ts = 0;

        try (FileInputStream input = new FileInputStream(fileName);
                InputStreamReader isr = new InputStreamReader(input);
                BufferedReader reader = new BufferedReader(isr)) {
            String line = reader.readLine();
            map.put(new Interval(new DateTime(), new DateTime()), "");
        }
}

SSCCE - , - try ( ) - . , ( fileName, , , ).

, , Joda-time 2.3 2.1, -, . , .

+4
1

:

JodaTime? , 2.0, Comparable (. ReadableInstant)? Pre 2.0 compareTo(Object), 2.0, compareTo(ReadableInstant), . . joda- :

"compareTo" "(Lorg/joda/time/ReadableInstant;) I" , org.joda.time.DateTime.

( upvotes. , JRuby-, JodaTime.)

+1

Source: https://habr.com/ru/post/1534642/


All Articles