Java Date/Calendar object strings, compare

2019-08-04 18:03发布

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!

5条回答
趁早两清
2楼-- · 2019-08-04 18:33

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 star\"
3楼-- · 2019-08-04 18:37

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.

查看更多
Ridiculous、
4楼-- · 2019-08-04 18:50

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.

查看更多
该账号已被封号
5楼-- · 2019-08-04 18:53

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.

查看更多
聊天终结者
6楼-- · 2019-08-04 18:57
  • 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.

查看更多
登录 后发表回答