Domanda

Let's assume that I have two dates like the following.

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45");
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45");

I want to compare these two dates to see whether firstDate is sooner, later or equal to secondDate.

I could try the following.

System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isBefore(secondDate));
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isAfter(secondDate));
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.equals(secondDate));

What is produced by this code is exactly what I want.

It produces the following output.

firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = true

firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = false

firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = false

I need to ignore the seconds and the milliseconds portion of these dates. I have tried to use the withSecondOfMinute(0) and withMillis(0) methods like the following.

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillis(0);
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillis(0);        

But it yielded the following output.

firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = false

firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = false

firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = true

The docs of the withSecondOfMinute() method describes.

Returns a copy of this datetime with the second of minute field updated. DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of second of minute changed.

And the docs of the method withMillis() says.

Returns a copy of this datetime with different millis. The returned object will be either be a new instance or this. Only the millis will change, the chronology and time zone are kept.

Comparing dates by ignoring the time portion completely can easily be done using DateTimeComparator.getDateOnlyInstance() roughly like the following.

if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)==0){}
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)<0){}
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)>0){}

How to compare two dates ignoring the specific instant in DateTime (seconds and milliseconds in this case)?

È stato utile?

Soluzione 2

I think you want to use DateTime#withMillisOfSecond() instead of DateTime#withMillis():

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillisOfSecond(0);
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillisOfSecond(0);

Setting DateTime#withMillis() to 0, will reset both your dates to 1/1/1970, and hence you get true for equals call on them.

Similarly, setting DateTime#withMillisOfDay() to 0, will set the time to midnight.

Altri suggerimenti

Another approach is to create a DateTimeComparator with minute as lower limit:

DateTimeComparator comparator = DateTimeComparator.getInstance(
         DateTimeFieldType.minuteOfHour());

which would ignore the second and millisecond parts of the objects being compared.

public static String getFromatDateTime(Date date) {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime( date );
    //gc.set( Calendar.HOUR_OF_DAY, 0 );
    //gc.set( Calendar.MINUTE, 0 );
    //block ignore second and millisecond
    gc.set( Calendar.SECOND, 0 );
    gc.set( Calendar.MILLISECOND, 0 );
    String strDate = sdfDate.format(gc.getTime());
    return strDate;
}
public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date now = new  Date();
    String currentDate = Testing.getFromatDateTime(now);
    String fullDate = "2015-12-07 14:53:39.30";
    String effDateStr = Testing.getFromatDateTime(sdfDate.parse(fullDate));

    System.out.println("Currennt Date: " + currentDate);
    System.out.println("Effective Date: " + effDateStr);
    System.out.println(currentDate.compareTo(effDateStr)==0);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top