Example usage for org.joda.time DateTime DateTime

List of usage examples for org.joda.time DateTime DateTime

Introduction

In this page you can find the example usage for org.joda.time DateTime DateTime.

Prototype

public DateTime(Object instant) 

Source Link

Document

Constructs an instance from an Object that represents a datetime.

Usage

From source file:ch.opentrainingcenter.model.training.filter.internal.FilterTrainingByDate.java

License:Open Source License

private long round(final Date date, final int hour, final int minute) {
    final DateTime dtStart = new DateTime(date);
    final int year = dtStart.getYear();
    final int monthOfYear = dtStart.getMonthOfYear();
    final int dayOfYear = dtStart.getDayOfMonth();
    final DateTime dt = new DateTime(year, monthOfYear, dayOfYear, hour, minute);
    return dt.getMillis();
}

From source file:ch.silviowangler.dox.DocumentServiceImpl.java

License:Apache License

@SuppressWarnings("unchecked")
private Object makeAssignable(AttributeDataType desiredDataType, Object valueToConvert) {

    if (valueToConvert instanceof ch.silviowangler.dox.api.Range && isRangeCompatible(desiredDataType)) {
        logger.debug("Found a range parameter. Skip this one");

        if (DATE.equals(desiredDataType)) {
            ch.silviowangler.dox.api.Range<DateTime> original = (ch.silviowangler.dox.api.Range<DateTime>) valueToConvert;
            return new Range<>(original.getFrom(), original.getTo());
        } else if (DOUBLE.equals(desiredDataType)) {
            ch.silviowangler.dox.api.Range<BigDecimal> original = (ch.silviowangler.dox.api.Range<BigDecimal>) valueToConvert;
            return new Range<>(original.getFrom(), original.getTo());
        } else if (INTEGER.equals(desiredDataType)) {
            ch.silviowangler.dox.api.Range<Integer> original = (ch.silviowangler.dox.api.Range<Integer>) valueToConvert;
            return new Range<>(original.getFrom(), original.getTo());
        } else if (LONG.equals(desiredDataType)) {
            ch.silviowangler.dox.api.Range<Long> original = (ch.silviowangler.dox.api.Range<Long>) valueToConvert;
            return new Range<>(original.getFrom(), original.getTo());
        } else if (SHORT.equals(desiredDataType)) {
            ch.silviowangler.dox.api.Range<Short> original = (ch.silviowangler.dox.api.Range<Short>) valueToConvert;
            return new Range<>(original.getFrom(), original.getTo());
        }/* ww  w. ja v  a2  s.com*/
        throw new IllegalArgumentException();
    }

    if (DATE.equals(desiredDataType) && valueToConvert instanceof String) {

        final String stringValueToConvert = (String) valueToConvert;
        String regexPattern;

        if (stringValueToConvert.matches("\\d{4}-\\d{2}-\\d{2}")) {
            regexPattern = YYYY_MM_DD;
        } else if (stringValueToConvert.matches("\\d{2}\\.\\d{2}\\.\\d{4}")) {
            regexPattern = DD_MM_YYYY;
        } else {
            logger.error("Unsupported format of a date string '{}'", stringValueToConvert);
            throw new UnsupportedOperationException("Unknown date format " + stringValueToConvert);
        }
        return DateTimeFormat.forPattern(regexPattern).parseDateTime(stringValueToConvert);
    } else if (DATE.equals(desiredDataType) && valueToConvert instanceof Date) {
        return new DateTime(valueToConvert);
    } else if (DOUBLE.equals(desiredDataType) && valueToConvert instanceof Double) {
        return BigDecimal.valueOf((Double) valueToConvert);
    } else if (DOUBLE.equals(desiredDataType) && valueToConvert instanceof String
            && ((String) valueToConvert).matches("(\\d.*|\\d.*\\.\\d{1,2})")) {
        return new BigDecimal((String) valueToConvert);
    } else if (DOUBLE.equals(desiredDataType) && valueToConvert instanceof Integer) {
        return BigDecimal.valueOf(Long.parseLong(String.valueOf(valueToConvert)));
    } else if (DOUBLE.equals(desiredDataType) && valueToConvert instanceof Long) {
        return BigDecimal.valueOf((Long) valueToConvert);
    } else if (CURRENCY.equals(desiredDataType) && valueToConvert instanceof String) {
        String value = (String) valueToConvert;
        return new AmountOfMoney(value);
    } else if (CURRENCY.equals(desiredDataType) && valueToConvert instanceof Money) {
        Money money = (Money) valueToConvert;
        return new AmountOfMoney(money.getCurrency(), money.getAmount());
    } else if (CURRENCY.equals(desiredDataType) && valueToConvert instanceof Map) {
        Map money = (Map) valueToConvert;
        return new AmountOfMoney(Currency.getInstance((String) money.get("currency")),
                new BigDecimal((String) money.get("amount")));
    }

    logger.error("Unable to convert data type '{}' and value '{}' (class: '{}')",
            new Object[] { desiredDataType, valueToConvert, valueToConvert.getClass().getCanonicalName() });
    throw new IllegalArgumentException("Unable to convert data type '" + desiredDataType + "' and value '"
            + valueToConvert + "' (Target class: '" + valueToConvert.getClass().getCanonicalName() + "')");
}

From source file:ch.thn.gedcom.GedcomHelper.java

License:Apache License

/**
 * Calculates the age of the person between the given birthDate and the toDate
 * //from  ww w  .j av a 2s . c o m
 * @param fromDate
 * @param toDate
 * @return
 */
public static int getAge(Date fromDate, Date toDate) {
    if (fromDate == null || toDate == null) {
        return 0;
    }

    DateMidnight bd = new DateMidnight(fromDate);
    DateTime now = new DateTime(toDate);
    Years age = Years.yearsBetween(bd, now);
    return age.getYears();
}

From source file:ch.thn.gedcom.GedcomHelper.java

License:Apache License

/**
 * Checks it the date (and time) given with <code>isBeforeOrAfter</code> is before or 
 * after the date (and time) given with <code>date</code>.
 * //from  w w w  .  ja  va2s  . c o  m
 * @param date
 * @param isBeforeOrAfter
 * @return {@link #BEFORE}, {@link #AFTER} or {@link #SAME}
 */
public static int isBeforeOrAfter(Date date, Date isBeforeOrAfter) {
    DateTime dtDate = new DateTime(date);
    DateTime dtIsBeforeOrAfter = new DateTime(isBeforeOrAfter);

    if (dtIsBeforeOrAfter.isBefore(dtDate)) {
        return BEFORE;
    } else if (dtIsBeforeOrAfter.isAfter(dtDate)) {
        return AFTER;
    } else {
        return SAME;
    }
}

From source file:ch.windmobile.server.mongo.MongoDataSource.java

License:Open Source License

private DateTime getLastUpdateDateTime(BasicDBObject lastDataJson) {
    return new DateTime(lastDataJson.getLong("_id") * 1000);
}

From source file:cherry.foundation.type.mybatis.JodaDateTimeTypeHandler.java

License:Apache License

@Override
public DateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
    Timestamp timestamp = rs.getTimestamp(columnName);
    if (timestamp == null) {
        return null;
    }// ww  w .  j  a v  a2 s .  c om
    return new DateTime(timestamp.getTime());
}

From source file:cherry.foundation.type.mybatis.JodaDateTimeTypeHandler.java

License:Apache License

@Override
public DateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    Timestamp timestamp = rs.getTimestamp(columnIndex);
    if (timestamp == null) {
        return null;
    }//w w  w  .  j  ava 2  s  . c  o m
    return new DateTime(timestamp.getTime());
}

From source file:cherry.foundation.type.mybatis.JodaDateTimeTypeHandler.java

License:Apache License

@Override
public DateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    Timestamp timestamp = cs.getTimestamp(columnIndex);
    if (timestamp == null) {
        return null;
    }/*from   ww  w . j  av  a 2 s  . com*/
    return new DateTime(timestamp.getTime());
}

From source file:cherry.foundation.type.querydsl.DateTimeType.java

License:Apache License

@Override
public DateTime getValue(ResultSet rs, int startIndex) throws SQLException {
    Timestamp timestamp = rs.getTimestamp(startIndex);
    if (timestamp == null) {
        return null;
    }//from  ww  w  . j a va 2 s.c  o  m
    return new DateTime(timestamp.getTime());
}

From source file:cl.usach.managedbeans.CreditosManagedBean.java

public int obtenerHorasDiff(Date inicio, Date fin) {
    DateTime dtI = new DateTime(inicio);
    DateTime dtF = new DateTime(fin);
    int tiempo = Hours.hoursBetween(dtI, dtF).getHours();
    return tiempo;
}