Example usage for java.util Date compareTo

List of usage examples for java.util Date compareTo

Introduction

In this page you can find the example usage for java.util Date compareTo.

Prototype

public int compareTo(Date anotherDate) 

Source Link

Document

Compares two Dates for ordering.

Usage

From source file:org.jasig.ssp.web.api.reports.PersonHistoryReportController.java

private static int sortDateDescending(Date date1, Date date2) {
    if (date1.compareTo(date2) < 0) {
        return 1;
    } else if (date1.compareTo(date2) > 0) {
        return -1;
    } else {//from ww  w  .ja  v a 2  s .c om
        return 0;
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.StandbyStorageRetentionManager.java

/**
 * List all directories that match the backup pattern.
 * Sort from oldest to newest./*from  w w w  . ja v a  2  s .co m*/
 */
static String[] getBackups(File origin) {
    File root = origin.getParentFile();
    final String originName = origin.getName();

    String[] backups = root.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            if (!name.startsWith(originName + File.pathSeparator) || name.equals(originName))
                return false;
            try {
                dateForm.parse(name.substring(name.indexOf(File.pathSeparator) + 1));
            } catch (ParseException pex) {
                return false;
            }
            return true;
        }
    });
    if (backups == null)
        return new String[0];

    Arrays.sort(backups, new Comparator<String>() {

        @Override
        public int compare(String back1, String back2) {
            try {
                Date date1 = dateForm.parse(back1.substring(back1.indexOf(File.pathSeparator) + 1));
                Date date2 = dateForm.parse(back2.substring(back2.indexOf(File.pathSeparator) + 1));
                // Sorting in reverse order, from later dates to earlier
                return -1 * date2.compareTo(date1);
            } catch (ParseException pex) {
                return 0;
            }
        }
    });
    return backups;
}

From source file:org.apache.hadoop.hdfs.server.namenode.NNStorageDirectoryRetentionManager.java

/**
 * List all directories that match the backup pattern.
 * Sort from oldest to newest.//from  ww w. j av a2 s. com
 */
static String[] getBackups(File origin) {
    File root = origin.getParentFile();
    final String originName = origin.getName();

    String[] backups = root.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            if (!name.startsWith(originName + File.pathSeparator) || name.equals(originName))
                return false;
            try {
                dateForm.get().parse(name.substring(name.indexOf(File.pathSeparator) + 1));
            } catch (ParseException pex) {
                return false;
            }
            return true;
        }
    });
    if (backups == null)
        return new String[0];

    Arrays.sort(backups, new Comparator<String>() {

        @Override
        public int compare(String back1, String back2) {
            try {
                Date date1 = dateForm.get().parse(back1.substring(back1.indexOf(File.pathSeparator) + 1));
                Date date2 = dateForm.get().parse(back2.substring(back2.indexOf(File.pathSeparator) + 1));
                // Sorting in reverse order, from later dates to earlier
                return -1 * date2.compareTo(date1);
            } catch (ParseException pex) {
                return 0;
            }
        }
    });
    return backups;
}

From source file:org.openmrs.module.pmtct.PMTCTModuleTag.java

public static String currentEncounterId(List<Encounter> encList, Date d1) {
    String res = "";
    for (Encounter enc : encList) {
        if (d1.compareTo(enc.getEncounterDatetime()) <= 0) {
            res = enc.getEncounterId().toString();
        }//from w ww.  j  a v  a2 s .c  om
    }

    return res;
}

From source file:org.wiztools.commons.DateUtil.java

/**
 * This is an inclusive method: returns true if the date is equal to startDate or endDate.
 * @param startDate The start date./*from   w  ww.j ava  2s. c o  m*/
 * @param endDate The end date.
 * @param date The date to verify.
 * @return true if the date falls between start date and end date.
 */
public static boolean isDateBetween(final Date startDate,
        final Date endDate,
        final Date date) {
    // check if end date is later than start date:
    if(startDate.compareTo(endDate) > 0) {
        throw new IllegalArgumentException("Start date cannot be greater than end date!");
    }
    if(date.compareTo(startDate) >= 0 && date.compareTo(endDate) <= 0) {
        return true;
    }
    return false;
}

From source file:org.openmrs.module.pmtct.PMTCTModuleTag.java

public static String encounterId(List<Encounter> encList, Date d1, Date d2) {
    String res = "";
    for (Encounter enc : encList) {
        if (d1.compareTo(enc.getEncounterDatetime()) <= 0 && d2.compareTo(enc.getEncounterDatetime()) >= 0) {
            res = enc.getEncounterId().toString();
        }/*from ww w. j a va  2s  .  co m*/
    }

    return res;
}

From source file:com.adsapient.shared.service.TimeService.java

public static final boolean isCampainPeriodActive(String beginDate, String endDate) {
    try {/*from  w ww  .j av  a2 s  .  com*/
        Date startDate = new Date(parseDateDDMMYYYY(beginDate, false));
        Date completeDate = new Date(parseDateDDMMYYYY(endDate, false));
        Date currentDate = Calendar.getInstance().getTime();

        if (currentDate.compareTo(startDate) < 0) {
            return false;
        }

        if (currentDate.compareTo(completeDate) > 0) {
            return false;
        }

        return true;
    } catch (IncorrectDateException ex) {
        System.err.println(ex.getCause());
    }

    return false;
}

From source file:org.rockyroadshub.planner.core.gui.calendar.FormPane.java

private static boolean compareTime(String start, String end) {
    try {/* w w  w  .  ja  va  2  s. co m*/
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        Date startTime = dateFormat.parse(start);
        Date endTime = dateFormat.parse(end);
        int timeComparison = endTime.compareTo(startTime);
        if (timeComparison == 0 || timeComparison == -1) {
            MainFrame.showErrorDialog((timeComparison == 0) ? START_EQ_END : START_VS_END);
            return false;
        }
        return true;
    } catch (ParseException ex) {
        MainFrame.showErrorDialog(ex.getMessage());
        return false;
    }
}

From source file:org.kuali.kfs.sys.util.KfsDateUtils.java

/**
 * Determines if the given date d1 is on the same day or an earlier day than the given date d2.
 * @param d1 a date/*from w w  w  .  j  a v  a2 s .  c  om*/
 * @param d2 another date, to compare the first date to
 * @return true if d1 is earlier or the same day as d2, false otherwise or if either value is null
 */
public static boolean isSameDayOrEarlier(Date d1, Date d2) {
    if (ObjectUtils.isNull(d1) || ObjectUtils.isNull(d2)) {
        return false;
    }
    if (isSameDay(d1, d2)) {
        return true;
    }
    return d1.compareTo(d2) < 0;
}

From source file:org.callistasoftware.netcare.core.api.impl.ScheduledActivityImpl.java

public static ScheduledActivity newFromEntity(ScheduledActivityEntity entity) {
    ScheduledActivityImpl scheduledActivity = new ScheduledActivityImpl();

    scheduledActivity.healthPlanName = entity.getActivityDefinitionEntity().getHealthPlan().getName();

    scheduledActivity.id = entity.getId();
    scheduledActivity.activityDefinition = ActivityDefinitionImpl
            .newFromEntity(entity.getActivityDefinitionEntity());

    Calendar cal = Calendar.getInstance();
    cal.setTime(entity.getScheduledTime());
    int day = cal.get(Calendar.DAY_OF_WEEK);
    scheduledActivity.day = new Option("weekday." + day, LocaleContextHolder.getLocale());

    cal.setTime(new Date());
    ApiUtil.dayBegin(cal);//  w w  w.  j  a v  a2  s  .com
    Date time = entity.getScheduledTime();

    scheduledActivity.due = (time.compareTo(cal.getTime()) < 0);
    scheduledActivity.date = ApiUtil.formatDate(time);

    scheduledActivity.time = ApiUtil.formatTime(time);

    if (entity.getReportedTime() != null) {

        Calendar reportedCal = Calendar.getInstance();
        reportedCal.setTime(entity.getReportedTime());

        int repDay = reportedCal.get(Calendar.DAY_OF_WEEK);

        Date reportedTime = entity.getReportedTime();
        scheduledActivity.reportedDay = new Option("weekday." + repDay, LocaleContextHolder.getLocale());
        scheduledActivity.reportedDate = ApiUtil.formatDate(reportedTime);
        scheduledActivity.reportedTime = ApiUtil.formatTime(reportedTime);
        scheduledActivity.reported = new StringBuilder(scheduledActivity.reportedDate).append(" ")
                .append(scheduledActivity.reportedTime).toString();

        final Calendar act = Calendar.getInstance();
        act.setTime(entity.getActualTime());

        int actWeekday = act.get(Calendar.DAY_OF_WEEK);
        scheduledActivity.actDay = new Option("weekday." + actWeekday, LocaleContextHolder.getLocale());
        scheduledActivity.actDate = ApiUtil.formatDate(entity.getActualTime());
        scheduledActivity.actTime = ApiUtil.formatTime(entity.getActualTime());

    }
    if (entity.getActualTime() != null) {
        scheduledActivity.actualTime = ApiUtil.formatDate(entity.getActualTime()) + " "
                + ApiUtil.formatTime(entity.getActualTime());
    }

    // Scheduled time within one week?
    final Date oneWeek = new DateTime(System.currentTimeMillis()).plusWeeks(1).toDate();
    final Date scheduled = entity.getScheduledTime();

    if (scheduled.before(oneWeek) && entity.getReportedTime() == null) {
        scheduledActivity.setReportingPossible(true);
    } else {
        scheduledActivity.setReportingPossible(false);
    }

    scheduledActivity.setExtra(entity.isExtra());

    List<ActivityItemValuesEntity> activityEntities = entity.getActivities();
    scheduledActivity.activityItemValues = new ActivityItemValues[activityEntities.size()];
    for (int i = 0; i < scheduledActivity.activityItemValues.length; i++) {
        ActivityItemValuesEntity activityItemValuesEntity = activityEntities.get(i);
        if (activityItemValuesEntity instanceof MeasurementEntity) {
            scheduledActivity.activityItemValues[i] = MeasurementImpl
                    .newFromEntity((MeasurementEntity) activityItemValuesEntity);
        } else if (activityItemValuesEntity instanceof EstimationEntity) {
            scheduledActivity.activityItemValues[i] = EstimationImpl
                    .newFromEntity((EstimationEntity) activityItemValuesEntity);
        } else if (activityItemValuesEntity instanceof YesNoEntity) {
            scheduledActivity.activityItemValues[i] = YesNoImpl
                    .newFromEntity((YesNoEntity) activityItemValuesEntity);
        } else if (activityItemValuesEntity instanceof TextEntity) {
            scheduledActivity.activityItemValues[i] = TextImpl
                    .newFromEntity((TextEntity) activityItemValuesEntity);
        }
    }
    scheduledActivity.rejected = entity.isRejected();
    scheduledActivity.patient = PatientBaseViewImpl
            .newFromEntity(entity.getActivityDefinitionEntity().getHealthPlan().getForPatient());
    scheduledActivity.note = entity.getNote();

    scheduledActivity.comments = new ActivityComment[entity.getComments().size()];
    for (int i = 0; i < entity.getComments().size(); i++) {
        scheduledActivity.comments[i] = ActivityCommentImpl.newFromEntity(entity.getComments().get(i));
    }

    return scheduledActivity;
}