简体   繁体   中英

Java Date/Calendar object strings, compare

I have date objects formatted like

2011/06/13 17:52:20

and being returned as strings. How would I compare this against another date formatted the same way. I want to determine which one is greater than, less than or equal to, for a conditional statement I am forming.

Without reinventing the wheel (or making several cases) when there might already be a framework for doing this

Thanks!

  • use SimpleDateFormat to parse
  • use compareTo(..) of the Date objects that are obtained

For example:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date1 = sdf.parse(string1);
Date date2 = sdf.parse(string2);

int result = date1.compareTo(date2);

The result is (from the java.util.Date documentation):

the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.

It looks to me like your date format is yyyy/mm/dd hh:mm:ss. If that's the case, you can do a string compare and it will give you an accurate greater/less/equal. The string is coded as most signficant to least significant.

My colleagues pointed out to me last week that yyyy-MM-dd HH:mm:ss strings is completely compatible with the ordering of the underlying dates (as long as the fields are all zero padded). So you can just to the compareTo on the String values if they are more readily available.

tl;dr

LocalDateTime.parse( "2011/06/13 17:52:20".replace( " " , "T" ) )
             .isBefore( LocalDateTime.now() )

true

Details

The other Answers are correct in that you can either (a) alphabetically compare those particular strings, or (b) chronologically compare after parsing into date-time objects.

And be aware that date-time formats do not have a “format”. They contain date-time information. They can generate a String that is in a particular format, but the date-time object and the string are separate and distinct.

The other Answers use the troublesome old date-time classes that are now legacy, supplanted with the java.time classes.

Your inputs lack info about offset-from-UTC and time zone. So we must parse them as LocalDateTime objects. To parse, replace the SPACE in the middle with a T to comply with the ISO 8601 standard for formatting strings that represent date-time values.

String input = "2011/06/13 17:52:20".replace( " " , "T" );
LocalDateTime ldtThen = LocalDateTime.parse( input ) ;
LocalDateTime ldtNow = LocalDateTime.now( ZoneId.of( "America/Montreal" ) ) ;

Compare.

boolean ldtThenIsBefore = ldtThen.isBefore( ldtNow );
boolean ldtThenIsAfter = ldtThen.isAfter( ldtNow );
boolean ldtThenIsEqual = ldtThen.isEqual( ldtNow );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

Although SimpleDateFormat allows one to parse text into a date object, you're much better off storing the date as a Date object and parsing it on display.

Create/Store Date objects and use their built-in compareTo() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM