简体   繁体   中英

issue with comparing two dates in joda time API

I have a below program to compare two dates.

I get timestamps that are date1 and currentTimestamp, here i need to compare only dates not the time values.But below program always returns -1.(value)

timestamp date1 = "2017-01-20 14:51:30.091"  // i get this from service call in this format

SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_TIMESTAMP_FORMAT);
Calendar calendar = Calendar.getInstance();
String formattedDate = dateFormat.format(calendar.getTime());
java.util.Date currentDate = dateFormat.parse(formattedDate);
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(currentDate.getTime());

int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , currentTimestamp );

How to compare only dates regardless of time. Please help me on this.

UPDATED:

i changed to below code

timestamp date1 = "2017-01-20 14:51:30.091"  // i get this from service call in this format
 LocalDate localDate = new LocalDate();
int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , localDate );

this gives me error saying "java.lang.IllegalArgumentException: No instant converter found for type: org.joda.time.LocalDate"

To compare DateTime in joda without time you have 2 options:

  • convert DateTime to LocalDate .
  • Use DateTimeComparator.getDateOnlyInstance

For example:

@Test
public void compareJodaTime() {
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");

    String today = "2017-01-20 14:51:30.091";  
    String tomorrow = "2017-01-21 14:51:30.091"; 

    DateTime now = DateTime.now();

    Assert.assertThat(now.toLocalDate().compareTo(DateTime.parse(today, dateTimeFormatter).toLocalDate()), Matchers.is(0));
    Assert.assertThat(now.toLocalDate().compareTo(DateTime.parse(tomorrow, dateTimeFormatter).toLocalDate()), Matchers.is(-1));
    Assert.assertThat(now.toLocalDate().isEqual(DateTime.parse(today, dateTimeFormatter).toLocalDate()), Matchers.is(true));
    Assert.assertThat(now.toLocalDate().isBefore(DateTime.parse(tomorrow, dateTimeFormatter).toLocalDate()), Matchers.is(true));


    Assert.assertThat(DateTimeComparator.getDateOnlyInstance().compare(now, DateTime.parse(today, dateTimeFormatter)), Matchers.is(0));
    Assert.assertThat(DateTimeComparator.getDateOnlyInstance().compare(now, DateTime.parse(tomorrow, dateTimeFormatter)), Matchers.is(-1));
}

This is NOT Joda solution...

Use: Apache library 'commons-lang3-xxjar' (DateUtils)

@Test
public void testCompare() {
 Date current = new Date();
 Date previous = DateUtils.addHours(DateUtils.addDays(new Date(), -1),5);
 assertEquals(1,DateUtils.truncatedCompareTo(current,previous,Calendar.DATE));
 assertEquals(0,DateUtils.truncatedCompareTo(current,current,Calendar.DATE));
 assertEquals(-1,DateUtils.truncatedCompareTo(previous,current,Calendar.DATE));
}

You can Try this code with JodaTime : DateTime date1 = DateTime.parse("2017-01-20 14:51:30.091", DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS")); DateTime now = new DateTime(); int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , now); DateTime date1 = DateTime.parse("2017-01-20 14:51:30.091", DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS")); DateTime now = new DateTime(); int value = DateTimeComparator.getDateOnlyInstance().compare(date1 , now);

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