Example usage for java.util Date equals

List of usage examples for java.util Date equals

Introduction

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

Prototype

public boolean equals(Object obj) 

Source Link

Document

Compares two dates for equality.

Usage

From source file:nl.strohalm.cyclos.utils.FormatObject.java

/**
 * Format an object//from w w  w . j  a  va2s  . co m
 */
public static String formatObject(final Object object, final String defaultValue, final String emptyValue) {
    if (object == null) {
        return defaultValue;
    }
    try {
        if (object instanceof Number) {
            if ((object instanceof Float) || (object instanceof Double) || (object instanceof BigDecimal)) {
                return FLOAT_FORMAT.format(((Number) object).doubleValue());
            }
            return INTEGER_FORMAT.format(((Number) object).longValue());
        } else if ((object instanceof Calendar) || (object instanceof Date)) {
            final Date date = (object instanceof Calendar) ? ((Calendar) object).getTime() : (Date) object;
            final Date truncated = DateUtils.truncate(date, Calendar.DATE);
            if (date.equals(truncated)) {
                return DATE_FORMAT.format(date);
            } else {
                return DATE_TIME_FORMAT.format(date);
            }
        } else if (object instanceof Collection<?> || object instanceof Map<?, ?>
                || object.getClass().isArray()) {
            if (object instanceof Iterator<?>) {
                return "<iterator>";
            } else if (Hibernate.isInitialized(object)) {
                Iterator<?> iterator;
                if (object instanceof Map<?, ?>) {
                    iterator = ((Map<?, ?>) object).entrySet().iterator();
                } else {
                    iterator = IteratorUtils.getIterator(object);
                }
                final StringBuilder sb = new StringBuilder();
                sb.append('[');
                while (iterator.hasNext()) {
                    sb.append(formatArgument(iterator.next()));
                    if (iterator.hasNext()) {
                        sb.append(", ");
                    }
                }
                sb.append(']');
                return sb.toString();
            } else {
                return "<uninitialized collection>";
            }
        } else if (object instanceof String) {
            return "".equals("object") ? emptyValue : (String) object;
        } else if (object instanceof Boolean) {
            return object.toString();
        } else if ((object instanceof Entity) && (object instanceof EntityReference)
                || !Hibernate.isInitialized(object)) {
            return ClassHelper.getClassName(EntityHelper.getRealClass((Entity) object)) + '#'
                    + ((Entity) object).getId();
        } else {
            return object.toString();
        }
    } catch (final Exception e) {
        return defaultValue;
    }
}

From source file:org.opentides.util.StringUtil.java

@Deprecated
public static boolean compareNullableDates(Date date1, Date date2) {
    if ((date1 == null) && (date2 == null))
        return true;
    if (date1 != null) {
        if (date1.equals(date2))
            return true;
        else/* ww  w . j  a v a  2 s  . co m*/
            return false;
    }
    return false;
}

From source file:nu.mine.kino.projects.utils.Utils.java

/**
 * baseDatestartDate/endDate?Bboolean??AOK?B
 * null??false/*from   ww  w  .  j a  va  2 s  .  c  o m*/
 * 
 * @param baseDate
 * @param startDate
 * @param endDate
 * @return
 */
static boolean isBetween(Date baseDate, Date startDate, Date endDate, boolean startFlag, boolean endFlag) {
    // ?WbN??A?B
    if (baseDate == null || startDate == null || endDate == null) {
        return false;
    }
    boolean isAfter = baseDate.after(startDate) || baseDate.equals(startDate);
    boolean isBefore = baseDate.before(endDate) || baseDate.equals(endDate);
    boolean isBetween = isAfter && isBefore;
    if (isBetween) {
        // startFlag?? tOKtrue.???Atfalse(ttrue)
        boolean startState = startFlag || !baseDate.equals(startDate);
        // endFlag?? tOKtrue.???Atfalse(ttrue)
        boolean endState = endFlag || !baseDate.equals(endDate);
        return isBetween && startState && endState;
    }
    return false;
}

From source file:org.nuclos.common2.DateUtils.java

/**
 * @param date is not altered./*from  w w  w . j av  a 2s. c o  m*/
 * @return Is the given date pure?
 * @precondition date != null
 * @postcondition result --> date.getHours() == 0
 * @postcondition result --> date.getMinutes() == 0
 * @postcondition result --> date.getSeconds() == 0
 * @see   #getPureDate(Date)
 */
public static boolean isPure(Date date) {
    final boolean result = date.equals(getPureDate(date));
    assert !result || date.getHours() == 0;
    assert !result || date.getMinutes() == 0;
    assert !result || date.getSeconds() == 0;
    return result;
}

From source file:playground.jbischoff.taxi.berlin.supply.TaxiStatusDataAnalyser.java

private static void writeIdleVehiclesByZoneAndStatus(Matrices statusMatrices, String idleVehicles, String start,
        String end) throws ParseException, IOException {

    Set<String> allZones = new HashSet<String>();
    for (Matrix matrix : statusMatrices.getMatrices().values()) {
        allZones.addAll(matrix.getFromLocations().keySet());
    }/*from   w  w w  . jav a  2  s . c om*/

    BufferedWriter writer = IOUtils.getBufferedWriter(idleVehicles);
    Date currentTime = STATUS_DATE_FORMAT.parse(start);
    Date endTime = STATUS_DATE_FORMAT.parse(end);

    //header
    writer.append("zone");
    while (!currentTime.equals(endTime)) {

        if (currentTime.getMinutes() == 00) {
            writer.append("\t" + STATUS_DATE_FORMAT.format(currentTime));

        }
        currentTime = getNextTime(currentTime);

    }

    for (String zone : allZones) {
        currentTime = STATUS_DATE_FORMAT.parse(start);
        endTime = STATUS_DATE_FORMAT.parse(end);
        double hourlyVehicles = 0;
        writer.newLine();
        writer.append(zone);
        while (!currentTime.equals(endTime)) {

            Matrix m = statusMatrices.getMatrix(STATUS_DATE_FORMAT.format(currentTime));
            if (m != null) {
                if (m.getFromLocations().containsKey(zone)) {
                    for (Entry entry : m.getFromLocEntries(zone)) {
                        switch (entry.getToLocation()) {
                        case "65":
                        case "70":
                        case "80":
                        case "83":
                        case "85": {
                            hourlyVehicles += entry.getValue();
                            break;
                        }
                        default:
                            break;

                        }
                    }
                }
            }
            if (zone.equals("1011401")) {
                System.out.println(STATUS_DATE_FORMAT.format(currentTime) + "\t" + hourlyVehicles);
            }
            if (currentTime.getMinutes() == 55) {
                writer.append("\t" + hourlyVehicles / 12.0);
                hourlyVehicles = 0;

            }

            currentTime = getNextTime(currentTime);
        }

    }
    writer.flush();
    writer.close();
}

From source file:com.haulmont.timesheets.global.DateTimeUtils.java

private static Set<String> holidayAsSeparateStrings(Holiday holiday, Date startDate, Date endDate,
        String format) {/*  w w  w. j  a v  a 2s  .  co  m*/
    Date start;
    Date end;
    if (holiday.getStartDate().getTime() >= startDate.getTime()) {
        start = holiday.getStartDate();
    } else {
        start = startDate;
    }
    if (holiday.getEndDate().getTime() <= endDate.getTime()) {
        end = holiday.getEndDate();
    } else {
        end = endDate;
    }

    if (start.equals(startDate) && end.equals(endDate)) {
        return Collections.emptySet();
    } else {
        Set<String> stringDates = new HashSet<>();
        DateFormat formatter = new SimpleDateFormat(format);
        while (start.getTime() <= end.getTime()) {
            stringDates.add(formatter.format(start));
            start = DateUtils.addDays(start, 1);
        }

        return stringDates;
    }
}

From source file:com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.Util.java

public static boolean compareJson(JsonElement e1, JsonElement e2) {
    // NOTE: if every property defined in e1 is in e2, the objects are
    // considered equal.

    if (e1 == null && e2 == null) {
        return true;
    }//from   ww  w. j  ava2 s  . c  o m

    if (e1 == null || e2 == null) {
        return false;
    }

    if (e1.getClass() != e2.getClass()) {
        return false;
    }

    if (e1 instanceof JsonPrimitive) {
        JsonPrimitive p1 = (JsonPrimitive) e1;
        JsonPrimitive p2 = (JsonPrimitive) e2;

        if (p1.isString()) {
            try {
                Date d1 = DateSerializer.deserialize(p1.getAsString());
                Date d2 = DateSerializer.deserialize(p2.getAsString());

                if (!d1.equals(d2) && !e1.equals(e2)) {
                    return false;
                }
            } catch (Throwable t) {
                if (!e1.equals(e2)) {
                    return false;
                }
            }
        } else if (!e1.equals(e2)) {
            return false;
        }
    } else if (e1 instanceof JsonArray) {
        JsonArray a1 = (JsonArray) e1;
        JsonArray a2 = (JsonArray) e2;

        if (a1.size() != a2.size()) {
            return false;
        }

        for (int i = 0; i < a1.size(); i++) {
            if (!compareJson(a1.get(i), a2.get(i))) {
                return false;
            }
        }
    } else if (e1 instanceof JsonObject) {
        JsonObject o1 = (JsonObject) e1;
        JsonObject o2 = (JsonObject) e2;

        Set<Entry<String, JsonElement>> entrySet1 = o1.entrySet();

        for (Entry<String, JsonElement> entry : entrySet1) {
            if (entry.getKey().toLowerCase(Locale.getDefault()).equals("id")) {
                continue;
            }

            String propertyName1 = entry.getKey();
            String propertyName2 = null;
            for (Entry<String, JsonElement> entry2 : o2.entrySet()) {
                if (propertyName1.toLowerCase(Locale.getDefault())
                        .equals(entry2.getKey().toLowerCase(Locale.getDefault()))) {
                    propertyName2 = entry2.getKey();
                }
            }

            if (propertyName2 == null) {
                return false;
            }

            if (!compareJson(entry.getValue(), o2.get(propertyName2))) {
                return false;
            }
        }
    }

    return true;
}

From source file:com.ibm.domino.das.resources.DocumentResource.java

static private String ifModifiedSince(Document document, final String ifModifiedSince) throws NotesException {
    String lastModifiedHeader = null;
    DateTime lastModifiedDateTime = document.getLastModified();
    if (lastModifiedDateTime != null) {
        Date lastModifiedDate = lastModifiedDateTime.toJavaDate();
        if (lastModifiedDate != null) {
            // Formats the given date according to the RFC 1123 pattern. 
            lastModifiedHeader = org.apache.http.impl.cookie.DateUtils.formatDate(lastModifiedDate);
            if (lastModifiedHeader != null) {
                if (ifModifiedSince != null) {
                    if (ifModifiedSince.equalsIgnoreCase(lastModifiedHeader)) {
                        throw new WebApplicationException(Response.Status.NOT_MODIFIED);
                    }/*ww w. ja v a  2s  .c om*/
                    try {
                        Date ifModifiedSinceDate = org.apache.http.impl.cookie.DateUtils
                                .parseDate(ifModifiedSince);
                        if (ifModifiedSinceDate.equals(lastModifiedDate)
                                || ifModifiedSinceDate.after(lastModifiedDate)) {
                            throw new WebApplicationException(Response.Status.NOT_MODIFIED);
                        }
                    } catch (DateParseException e) {
                        // If we can not parse the If-Modified-Since then continue.
                        DAS_LOGGER.info("Can not parse the If-Modified-Since header."); // $NON-NLS-1$
                    }
                }
            }
        }
    }
    return lastModifiedHeader;
}

From source file:org.apache.lens.cube.metadata.DateUtil.java

public static CoveringInfo getMonthlyCoveringInfo(Date from, Date to) {
    // Move 'from' to end of month, unless its the first day of month
    boolean coverable = true;
    if (!from.equals(DateUtils.truncate(from, MONTH))) {
        from = DateUtils.addMonths(DateUtils.truncate(from, MONTH), 1);
        coverable = false;/* w  w  w.j av a  2 s . com*/
    }

    // Move 'to' to beginning of next month, unless its the first day of the month
    if (!to.equals(DateUtils.truncate(to, MONTH))) {
        to = DateUtils.truncate(to, MONTH);
        coverable = false;
    }

    int months = 0;
    while (from.before(to)) {
        from = DateUtils.addMonths(from, 1);
        months++;
    }
    return new CoveringInfo(months, coverable);
}

From source file:com.momathink.common.tools.ToolMonitor.java

/**
 * ?/*  w  w  w .  ja v a  2s. c  o  m*/
 * @param date ,(?:)
 * @return =true   =false
 */
public static boolean whetherExpired(Date date) {
    log.info("?: ?" + (expirationDate) + "      ?" + (date));
    boolean result = false;
    if (null == date || null == expirationDate || date.equals(expirationDate)) {//|| ToolDateTime.format(expirationDate, ToolDateTime.pattern_ymd).equals(ToolDateTime.format(date,ToolDateTime.pattern_ymd))
        result = true;
    } else {
        //b.before(a)ba??true
        result = expirationDate.before(date);
    }
    log.info("?: " + (result) + "(=true =false)");
    return result;
}