Is there a way to compare two calendar objects, but ignore milliseconds?
I have written a test case that compared two calendar objects, but there is a p
I'd recommend using Joda Time if you are performing anything beside the basic date manipulations. In your case you can truncate the dates like so and then compare :
DateTime dateTime = new DateTime().millisOfDay().roundFloorCopy();
clearMilis(date1).compareTo(clearMilis(date2))
/**
* @param date date
*
* @return truncated miliseconds
*/
@Nonnull
public static Date clearMillis(final @Nonnull Date date)
{
DateTime result = new DateTime(date);
return result.minusMillis(result.getMillisOfSecond()).toDate();
}
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 );
//gc.set( Calendar.SECOND, 0 );
//block ignore millisecond
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);
}
Remove milliseconds from your calendar
cal.set(Calendar.MILLISECOND, 0);