Comparing Dates Ignoring Seconds and Millisecond Moment of DateTime in Iodine

Suppose I have two dates, such as:

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 if it will be firstDateearlier, later, or equal 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 this code creates is exactly what I want.

It outputs the following result.

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 millisecond part of these dates. I tried using methods withSecondOfMinute(0)and withMillis(0)as shown below.

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 he gave the following result.

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

Method documents withSecondOfMinute()describe.

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

withMillis().

datetime . , . , .

, , DateTimeComparator.getDateOnlyInstance() .

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

, DateTime ( )?

+5
3

, DateTime#withMillisOfSecond() 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);

DateTime#withMillis() 0 reset 1/1/1970, , , true equals.

, DateTime#withMillisOfDay() 0, midnight.

+6

DateTimeComparator :

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

.

+8
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);
}
0
source

All Articles