It is necessary to compare data by date, but you can compare by string:

dateFormat = new SimpleDateFormat("dd.MM.yy"); date = dateFormat.parse(dateTime.getDay() + "." + dateTime.getMonth() + "." + dateTime.getYear()); dateFromFile = dateFormat.parse(filename.split(".")[0]); 

They can be translated into the string, and the date, and both comparisons will be correct. But which comparison will work faster?

  • one
    String comparisons generally have the right to be wrong. For example, the lexicographic comparison of even numbers is not ice: "01/10/01"> "2.01.01" If your day is first written in your format, then everything is generally rotten: "01/31/01"> "01/11/99" - VladD

2 answers 2

By date, naturally! The date, in fact, is a wrapper for Long. And comparing dates is nothing more than comparing LONGs. And comparing the date by casting it to the string is generally a perversion!

  • one
    Comparing through the date is right and beautiful. But it must be remembered that converting a string to a date is a "heavy operation." Heavy enough. And if you need to sort several hundred records by date, then it is better to first convert all dates, and then sort. And if in the comparator to do the conversion every time, it will be very long. - KoVadim
  • one
    @KoVadim I think it will be right to just store the dates in a non-stock form, but to convert them directly on the screen) And then there will be no problems with sorting! - Zakhar Zolotarev

You can also translate and compare to unicstaim. Often I do.