Comparing Date Strings in Java

Therefore, I use dateString1.compareTo(dateString2) , which performs lexicographic string comparisons based on the Unicode value of each character and returns an int. Here is a sample code.

 String dateString1 = "05-12-2012"; String dateString2 = "05-13-2012"; if (dateString1.compareTo(dateString2) <=0){ System.out.println("dateString1 is an earlier date than dateString2"); } 

Is this the wrong approach for comparing dates in Java?

In my tests, I did not encounter a situation where I received an unexpected result. I really don't want to create a Date object from a string unless I need it because I am doing this inside a long loop.

Editing a Ninja Taking away the answers below, there is nothing wrong with comparing dates as a string if it is in yyyyMMdd format, but if it is in any other format, this will obviously lead to an error.

Actually, I have a date string in yyyyMMdd format in my actual code. (I printed the wrong format in the above example). So for now, I just leave the code as it is, and add a few lines of comments to justify my decision.

But now I see that the comparison of such strings is very limited, and I will encounter errors if dba decides to change the date format along the way, which I do not see.

+7
source share
6 answers

I suggest you do the right thing ( as described here ) and convert to suitable Date objects for comparison. Worry about the impact of performance if and when it really affects your application (which probably won't).

+8
source

Using strings to process dates in Java is not always the best option. For example, when it is a jump , there is an extra day in February. Since the lines may seem correct, it is more advisable to do the conversion . Java checks for the correct date.

You can convert strings to dates using the SimpleDateFormat class.

 public static void main(String[] args) throws ParseException { String dateString1 = "05-12-2012"; String dateString2 = "05-13-2012"; SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); Date date1 = format.parse(dateString1); Date date2 = format.parse(dateString2); if (date1.compareTo(date2) <= 0) { System.out.println("dateString1 is an earlier date than dateString2"); } } 

To find out what options are allowed for checking Format settings (Java β„’ Tutorials> Formation)>

+17
source

This is pretty bad, because now you can not cope with the change in the year.

If you so wish, you can format the date as YYYY-MM-DD so that the new year does not spoil it.

+4
source

It’s bad to use alphabet rules for date processing, mainly because you run into problems when things are sorted differently according to alphabet and number system

For the alphabet

 01-02-2011 comes before 01-1-2011 (because 0 in the date field is before 1 in the other date field) 

For a numerical system

 01, 02, 2011 comes after 01, 1, 2011 because all fields are being compared like numbers 

Date objects extend the numerical comparison to know which fields have an advantage when comparing, so you don’t get an earlier month by putting the date β€œearlier” than another, which actually happens in the last month but earlier year.

If you have strict control over the date format, you can agree on dates so that they also comply with alphabetical rules; however, you run the risk that your entire program will fail if you accidentally enter an incorrect date.

A typical way to do this (not recommended, use comparisons other than String Date)

 YYYYMMDD (year)(month)(day) all zero-padded. 

The last method is included mainly because you will eventually see it in the wild and must recognize it for what: an attempt to process dates without an appropriate date library (aka smart hack).

+2
source

if you are doing only one reading of each date, then YYYYMMDD (not MMDDYYYY, as you did) may be the most optimal solution. however, when you plan to process each date more than once (for example, sort them), it is undoubtedly better to change them to an object that can be compared faster than a string (for example, date).

+1
source

As discussed, it is usually best to work with date objects rather than strings.

java.time

Other answers use old obsolete time classes that have been poorly designed, confusing, and complex. They lack a class to really be valuable only for a date without time and without a time zone.

Instead, use java.time built in to Java 8 and later. See Oracle Tutorial . Most of the functionality of java.time is ported to Java 6 and 7 in ThreeTen-Backport and further adapted for Android in ThreeTenABP .

 String input = "05-12-2012"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM-dd-yyyy" ); LocalDate ld = LocalDate.parse( input , formatter ); 

LocalDate implements compareTo . In addition, you can call methods equal to, isBefore, isAfter.

 Boolean isEarlier = ld.isBefore( someOtherLocalDate ); 
+1
source

All Articles