0

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"

daisy
  • 357
  • 3
  • 7
  • 22
  • 1
    Hmm. To work with Joda time, don't use `Calendar`, `SimpleDateFormat`, `java.util.Date` or `java.sql.Timestamp`. Use [Joda-time classes](http://www.joda.org/joda-time/quickstart.html) *only*. – RealSkeptic Jan 20 '17 at 09:56
  • 2
    maybe I missread this, but where is Joda in the code you posted?? – ΦXocę 웃 Пepeúpa ツ Jan 20 '17 at 09:56
  • Possible duplicate of [How to compare two Dates without the time portion?](http://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without-the-time-portion) – yannicuLar Jan 20 '17 at 10:20
  • @RealSkeptic I changed to localdate to get current date, but it gives error "java.lang.IllegalArgumentException: No instant converter found for type: org.joda.time.LocalDate" – daisy Jan 20 '17 at 10:21
  • Hint: go and built a **real** [mcve] for us please. – GhostCat Jan 20 '17 at 10:22
  • 3
    What is `timestamp`? It looks like it's supposed to be a class or type name of some sort, but you're assigning it a string? Please post *real* code. And as was mentioned, a [mcve] would be best. – RealSkeptic Jan 20 '17 at 10:24

3 Answers3

0

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));
}
Vlad Bochenin
  • 3,007
  • 1
  • 20
  • 33
0

This is NOT Joda solution...

Use: Apache library 'commons-lang3-x.x.jar' (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));
}
Juha Hanka
  • 639
  • 6
  • 5
0

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);

Opv
  • 440
  • 1
  • 3
  • 17