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.apigw.authserver.web.controller.TokensControllerTest.java

@Test
public void testGetTokenStatus() throws Exception {
    Map<String, Object> tokenMap = tokensController.getTokenStatus(null);
    String issueDateString = (String) tokenMap.get("issueDate");
    Date issueDate = tokensController.sdf.parse(issueDateString);
    assertEquals(0, issueDate.compareTo(checkDate));
    String accessTokenExpiresString = (String) tokenMap.get("accessTokenExpires");
    Date accessTokenExpires = tokensController.sdf.parse(accessTokenExpiresString);
    assertEquals(0, accessTokenExpires.compareTo(checkDate));
    List<String> scopes = (List<String>) tokenMap.get("scope");
    assertEquals(2, scopes.size());/*from   www  .j av a  2s .  com*/
    assertEquals(TOKEN_VALUE, tokenMap.get("accessToken"));
}

From source file:graficos.GenerarGraficoPromociones.java

public void crear() throws ParseException {
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    ArrayList<String> reservaciones = new ArrayList();
    try {/*  w  w  w . j  a v a 2s. c  om*/
        Dba db = new Dba(pathdb);
        db.conectar();
        String sql = "select FechaReservacion from Reservacion join Habitacion on Reservacion.IdHabitacion=Habitacion.IdHabitacion "
                + " where Habitacion.IdCategoria=" + idcategoria;
        db.prepare(sql);
        db.query.execute();
        ResultSet rs = db.query.getResultSet();

        while (rs.next()) {
            reservaciones.add(rs.getString(1));
        }
        db.desconectar();
    } catch (Exception e) {

    }
    ArrayList<String> promociones = new ArrayList();
    try {
        Dba db = new Dba(pathdb);
        db.conectar();
        String sql = "select Tipo, FechaInicio, FechaFin,Porcentaje from CategoriaDescuentoPromocion where IdCategoria="
                + idcategoria;
        db.prepare(sql);
        db.query.execute();
        ResultSet rs = db.query.getResultSet();
        while (rs.next()) {
            promociones.add(
                    rs.getString(1) + "," + rs.getString(2) + "," + rs.getString(3) + "," + rs.getString(4));
        }
        db.desconectar();
    } catch (Exception e) {

    }
    int[] count = new int[promociones.size() + 1];
    for (int i = 0; i < reservaciones.size(); i++) {
        int si = 0;
        for (int j = 0; j < promociones.size(); j++) {
            String[] partes = promociones.get(j).split(",");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date fechain = format.parse(partes[1]);
            Date fechafi = format.parse(partes[2]);
            Date fechare = format.parse(reservaciones.get(i));
            if (fechare.compareTo(fechain) >= 0 && fechare.compareTo(fechafi) <= 0) {
                si++;
                count[j]++;
            }
        }
        if (si == 0) {
            count[promociones.size()]++;
        }
    }
    for (int j = 0; j < count.length; j++) {
        if (j == promociones.size()) {
            pieDataset.setValue("Precio Normal", new Integer(count[j]));
        } else {
            String[] partes = promociones.get(j).split(",");
            pieDataset.setValue("desc: " + partes[3] + "%", new Integer(count[j]));
        }

    }
    JFreeChart chart = ChartFactory.createPieChart(
            "Porcentaje de Reservaciones Precio Normal vs Promociones y Descuentos", pieDataset, true, true,
            false);
    PiePlot plot = (PiePlot) chart.getPlot();
    PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0}: {1} ({2})",
            new DecimalFormat("0"), new DecimalFormat("0%"));
    plot.setLabelGenerator(gen);
    try {
        ChartUtilities.saveChartAsJPEG(new File(path), chart, 500, 300);
    } catch (Exception ee) {
        System.err.println(ee.toString());
        System.err.println("Problem occurred creating chart.");
    }
}

From source file:com.adaptris.core.services.metadata.compare.CompareTimestamps.java

@Override
public MetadataElement compare(MetadataElement firstItem, MetadataElement secondItem) throws ServiceException {
    MetadataElement result = new MetadataElement();
    result.setKey(getResultKey());//from ww  w  .j  a  v  a 2  s .co  m
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat());
        Date firstDate = sdf.parse(firstItem.getValue());
        Date secondDate = sdf.parse(secondItem.getValue());
        result.setValue(String.valueOf(firstDate.compareTo(secondDate)));
    } catch (ParseException e) {
        throw ExceptionHelper.wrapServiceException(e);
    }
    return result;
}

From source file:org.yamj.core.service.staging.StagingService.java

@Transactional(propagation = Propagation.REQUIRED)
public void storeStageDirectory(StageDirectoryDTO stageDirectoryDTO, Library library) {
    // normalize the directory path by using URI
    String normalized = FilenameUtils.normalizeNoEndSeparator(stageDirectoryDTO.getPath(), true);

    StageDirectory stageDirectory = stagingDao.getStageDirectory(normalized, library);
    if (stageDirectory == null) {
        stageDirectory = new StageDirectory();
        stageDirectory.setDirectoryPath(normalized);
        stageDirectory.setLibrary(library);
        stageDirectory.setStatus(StatusType.NEW);
        stageDirectory.setDirectoryDate(new Date(stageDirectoryDTO.getDate()));

        // getById parent stage directory
        int lastIndex = normalized.lastIndexOf('/');
        if (lastIndex > 0) {
            String parentPath = normalized.substring(0, lastIndex);
            StageDirectory parent = stagingDao.getStageDirectory(parentPath, library);
            if (parent != null) {
                stageDirectory.setParentDirectory(parent);
            }//from  w ww  . ja v  a  2  s .co m
        }

        stagingDao.saveEntity(stageDirectory);
    } else {
        Date newDate = new Date(stageDirectoryDTO.getDate());
        if (newDate.compareTo(stageDirectory.getDirectoryDate()) != 0) {
            stageDirectory.setDirectoryDate(new Date(stageDirectoryDTO.getDate()));
            stageDirectory.setStatus(StatusType.UPDATED);
            stagingDao.updateEntity(stageDirectory);
        }
    }

    for (StageFileDTO stageFileDTO : stageDirectoryDTO.getStageFiles()) {
        String baseName = FilenameUtils.getBaseName(stageFileDTO.getFileName());
        String extension = FilenameUtils.getExtension(stageFileDTO.getFileName());
        if (StringUtils.isBlank(baseName) || StringUtils.isBlank(extension)) {
            // no valid baseName or extension
            continue;
        }

        StageFile stageFile = stagingDao.getStageFile(baseName, extension, stageDirectory);
        if (stageFile == null) {
            // create new stage file entry
            stageFile = new StageFile();
            stageFile.setBaseName(baseName);
            stageFile.setExtension(extension);
            stageFile.setFileDate(new Date(stageFileDTO.getFileDate()));
            stageFile.setFileSize(stageFileDTO.getFileSize());
            stageFile.setStageDirectory(stageDirectory);
            stageFile.setFileType(filenameScanner.determineFileType(extension));
            stageFile
                    .setFullPath(FilenameUtils.concat(stageDirectoryDTO.getPath(), stageFileDTO.getFileName()));
            stageFile.setStatus(StatusType.NEW);
            stagingDao.saveEntity(stageFile);
        } else {
            Date newDate = new Date(stageFileDTO.getFileDate());
            if ((newDate.compareTo(stageFile.getFileDate()) != 0)
                    || (stageFile.getFileSize() != stageFileDTO.getFileSize())) {
                stageFile.setFileDate(new Date(stageFileDTO.getFileDate()));
                stageFile.setFileSize(stageFileDTO.getFileSize());
                if (!StatusType.DUPLICATE.equals(stageFile.getStatus())) {
                    // mark as updated if no duplicate
                    stageFile.setStatus(StatusType.UPDATED);
                }
                stagingDao.updateEntity(stageFile);
            }
        }
    }
}

From source file:Contract.AbstractContract.java

/**
 * //from   www .  j  a va  2 s .  com
 * @param workPlace - ? 
 * @param post - ?
 * @param validDate -  ? 
 * @throws WrongNumberValueException 
 */
public AbstractContract(String workPlace, String post, Date validDate) throws WrongNumberValueException {

    if (validDate.compareTo(new Date()) <= 0) {
        throw new WrongNumberValueException(
                "? ?  ?  ? ??");
    } else if (post.length() == 0) {
        throw new WrongNumberValueException(
                "? ?   ? ?");
    }

    this.workPlace = workPlace;
    this.post = post;
    this.payments = new ArrayList<AbstractPayment>();
    contractUniqueId++;
    this.forever = false;
}

From source file:org.eclipse.licensing.base.LicenseKey.java

public boolean isWithinExpirationDate() {
    String expirationDate = getExpirationDate();
    if (expirationDate == null) {
        // no date restriction
        return true;
    }// w  ww  .  j av a 2  s . c om

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date date = format.parse(getExpirationDate() + " 23:59:59");
        return date.compareTo(new Date()) > 0;
    } catch (ParseException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.yukthi.validators.LessThanValidator.java

@Override
public boolean isValid(Object bean, Object fieldValue) {
    Object otherValue = null;/* ww  w.  j a v a  2 s .c om*/

    try {
        otherValue = PropertyUtils.getSimpleProperty(bean, lessThanField);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new IllegalStateException("Invalid/inaccessible property \"" + lessThanField
                + "\" specified with matchWith validator in bean: " + bean.getClass().getName());
    }

    if (fieldValue == null || otherValue == null || !fieldValue.getClass().equals(otherValue.getClass())) {
        return true;
    }

    if (otherValue instanceof Number) {
        return (((Number) fieldValue).doubleValue() < ((Number) otherValue).doubleValue());
    }

    if (otherValue instanceof Date) {
        Date dateValue = DateUtils.truncate((Date) fieldValue, Calendar.DATE);
        Date otherDateValue = DateUtils.truncate((Date) otherValue, Calendar.DATE);

        return (dateValue.compareTo(otherDateValue) < 0);
    }

    return true;
}

From source file:Applicant.java

public double Compare_Dates(Date dob, Date dob1) {
    if (dob.compareTo(dob1) == 0)
        return 1.0;
    return 0;//from  w w  w.j av  a2 s.  co m
}

From source file:com.yukthi.validators.GreaterThanValidator.java

@Override
public boolean isValid(Object bean, Object fieldValue) {
    //obtain field value to be compared
    Object otherValue = null;//  ww w. j av  a2 s  .  c  o m

    try {
        otherValue = PropertyUtils.getSimpleProperty(bean, greaterThanField);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new IllegalStateException("Invalid/inaccessible property \"" + greaterThanField
                + "\" specified with matchWith validator in bean: " + bean.getClass().getName());
    }

    //if other value is null or of different type
    if (fieldValue == null || otherValue == null || !fieldValue.getClass().equals(otherValue.getClass())) {
        return true;
    }

    //number comparison
    if (otherValue instanceof Number) {
        return (((Number) fieldValue).doubleValue() > ((Number) otherValue).doubleValue());
    }

    //date comparison
    if (otherValue instanceof Date) {
        Date dateValue = DateUtils.truncate((Date) fieldValue, Calendar.DATE);
        Date otherDateValue = DateUtils.truncate((Date) otherValue, Calendar.DATE);

        return (dateValue.compareTo(otherDateValue) > 0);
    }

    return true;
}

From source file:com.yukthi.validators.LessThanEqualsValidator.java

@Override
public boolean isValid(Object bean, Object fieldValue) {
    //fetch other field value
    Object otherValue = null;/*from  w w w  .j a  v  a2 s . c o m*/

    try {
        otherValue = PropertyUtils.getSimpleProperty(bean, lessThanField);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new IllegalStateException("Invalid/inaccessible property \"" + lessThanField
                + "\" specified with matchWith validator in bean: " + bean.getClass().getName());
    }

    //if value is null or of different data type
    if (fieldValue == null || otherValue == null || !fieldValue.getClass().equals(otherValue.getClass())) {
        return true;
    }

    //number comparison
    if (otherValue instanceof Number) {
        return (((Number) fieldValue).doubleValue() <= ((Number) otherValue).doubleValue());
    }

    //date comparison
    if (otherValue instanceof Date) {
        Date dateValue = DateUtils.truncate((Date) fieldValue, Calendar.DATE);
        Date otherDateValue = DateUtils.truncate((Date) otherValue, Calendar.DATE);

        return (dateValue.compareTo(otherDateValue) <= 0);
    }

    return true;
}