Example usage for org.joda.time Period Period

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

Introduction

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

Prototype

public Period() 

Source Link

Document

Creates a new empty period with the standard set of fields.

Usage

From source file:fr.cls.atoll.motu.library.converter.jaxb.JodaPeriodAdapter.java

License:Open Source License

/**
 * Convert a given string date representation into an instance of Joda time date.
 * /*from w  w  w.j av a2  s. c  om*/
 * @param s the string to convert into a date.
 * @return a {@link DateTime} instance.
 */
@Override
public Period unmarshal(String s) {
    try {
        return JodaPeriodAdapter.PERIOD_FORMATER.parsePeriod(s);
    } catch (IllegalArgumentException e) {
        LOGGER.warn("Failed to parse period from " + s, e);
        return new Period();
    }
}

From source file:il.ac.technion.datacenter.sla.SLA.java

/**
 * @param billingPeriod E.g: <code>new Period().withYears(1);</code>
 *//*  w  ww.  j  av a 2 s  .  c om*/
public SLA(Period billingPeriod) {
    this.billingPeriod = billingPeriod;
    this.totalDownTime = new Period();
}

From source file:model.SqlInterface.java

/**
 * Keep a running total of the time spent on tasks on a specific date. 
 * This can also then be used to calculate the time spent on tasks this week.
 * /*from   ww w .  ja v  a 2  s  . com*/
 * @param date Today's date
 * 
 * @return The total time spent on tasks today.
 */
public Period getTallyForDate(DateTime date) {
    Period tally = new Period();
    ResultSet rs;

    try {
        rs = statementHandler
                .executeQuery("select * from timelord where date = '" + Time.getReferableDate(date) + "';");
        while (rs.next()) {
            tally = new Period(tally).plus(new Period(rs.getObject(4)));
        }

        rs.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return tally;
}

From source file:model.SqlInterface.java

/**
 * Using today's date and the date of the start of the week, determine how
 * much time has been spent on tasks so far this week.
 * //w  ww  .j  a  v  a  2  s .  c om
 * @param startDate The start date of the period.
 * @param endDate The end date of the period.
 * 
 * @return The total time spent on tasks this within the given period.
 */
public Period getWeekTally(DateTime startDate, DateTime endDate) {
    Period tally = new Period();
    // Need mutable to perform arithmetic
    MutableDateTime start = startDate.toMutableDateTime();
    // Need mutable to perform arithmetic
    MutableDateTime end = endDate.toMutableDateTime();
    // Only have 'isBefore' so add one day to make it equivalent to
    // 'isBeforeOrOnThisDay'
    end.addDays(1);

    ResultSet rs;

    while (start.isBefore(end)) {
        try {
            rs = statementHandler.executeQuery(
                    "select * from timelord where date = '" + Time.getReferableDate(start.toDateTime()) + "';");
            while (rs.next()) {
                // There should only be one for day and 7 for week
                tally = new Period(tally).plus(new Period(rs.getObject(4)));
            }

            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        start.addDays(1);
    }

    return tally;
}

From source file:nz.co.gregs.dbvolution.internal.datatypes.DateRepeatImpl.java

License:Apache License

/**
 *
 * @param intervalStr/*from  ww  w .  j av a 2s. c o  m*/
 * @return the DateRepeat value represented by the String value
 */
public static Period parseDateRepeatFromGetString(String intervalStr) {
    if (intervalStr == null || intervalStr.length() == 0) {
        return null;
    }
    System.out.println("DBV INTERVAL: " + intervalStr);
    Period interval = new Period();
    interval = interval.withYears(getYearPart(intervalStr));
    interval = interval.withMonths(getMonthPart(intervalStr));
    interval = interval.withDays(getDayPart(intervalStr));
    interval = interval.withHours(getHourPart(intervalStr));
    interval = interval.withMinutes(getMinutePart(intervalStr));
    interval = interval.withSeconds(getSecondPart(intervalStr));
    interval = interval.withMillis(getMillisecondPart(intervalStr));
    return interval;
}

From source file:org.apache.arrow.vector.IntervalDayVector.java

License:Apache License

/**
 * Same as {@link #get(int)}./*  ww w  .  ja  v  a2s .com*/
 *
 * @param index   position of element
 * @return element at given index
 */
public Period getObject(int index) {
    if (isSet(index) == 0) {
        return null;
    } else {
        final int startIndex = index * TYPE_WIDTH;
        final int days = valueBuffer.getInt(startIndex);
        final int milliseconds = valueBuffer.getInt(startIndex + MILLISECOND_OFFSET);
        final Period p = new Period();
        return p.plusDays(days).plusMillis(milliseconds);
    }
}

From source file:org.apache.arrow.vector.IntervalYearVector.java

License:Apache License

/**
 * Same as {@link #get(int)}.//from  ww  w  . ja v a  2s. c om
 *
 * @param index   position of element
 * @return element at given index
 */
public Period getObject(int index) {
    if (isSet(index) == 0) {
        return null;
    } else {
        final int interval = valueBuffer.getInt(index * TYPE_WIDTH);
        final int years = (interval / org.apache.arrow.vector.util.DateUtility.yearsToMonths);
        final int months = (interval % org.apache.arrow.vector.util.DateUtility.yearsToMonths);
        final Period p = new Period();
        return p.plusYears(years).plusMonths(months);
    }
}

From source file:org.apache.beam.sdk.extensions.sql.impl.interpreter.operator.date.BeamSqlDatetimeMinusIntervalExpression.java

License:Apache License

private Period intervalToPeriod(BeamSqlPrimitive operand) {
    BigDecimal intervalValue = operand.getDecimal();
    SqlTypeName intervalType = operand.getOutputType();

    int numberOfIntervals = intervalValue.divide(TimeUnitUtils.timeUnitInternalMultiplier(intervalType))
            .intValueExact();/*from  w  w w  . j  a  v a  2s. c o m*/

    return new Period().withField(durationFieldType(intervalType), numberOfIntervals);
}

From source file:org.apache.drill.exec.planner.common.DrillValuesRelBase.java

License:Apache License

private static void writeLiteral(RexLiteral literal, JsonOutput out) throws IOException {
    switch (literal.getType().getSqlTypeName()) {
    case BIGINT:/* ww  w.  ja  v  a  2s .  c o  m*/
        if (isLiteralNull(literal)) {
            out.writeBigIntNull();
        } else {
            out.writeBigInt(
                    (((BigDecimal) literal.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP)).longValue());
        }
        return;

    case BOOLEAN:
        if (isLiteralNull(literal)) {
            out.writeBooleanNull();
        } else {
            out.writeBoolean((Boolean) literal.getValue());
        }
        return;

    case CHAR:
        if (isLiteralNull(literal)) {
            out.writeVarcharNull();
        } else {
            // Since Calcite treats string literals as fixed char and adds trailing spaces to the strings to make them the
            // same length, here we do an rtrim() to get the string without the trailing spaces. If we don't rtrim, the comparison
            // with Drill's varchar column values would not return a match.
            // TODO: However, note that if the user had explicitly added spaces in the string literals then even those would get
            // trimmed, so this exposes another issue that needs to be resolved.
            out.writeVarChar(((NlsString) literal.getValue()).rtrim().getValue());
        }
        return;

    case DOUBLE:
        if (isLiteralNull(literal)) {
            out.writeDoubleNull();
        } else {
            out.writeDouble(((BigDecimal) literal.getValue()).doubleValue());
        }
        return;

    case FLOAT:
        if (isLiteralNull(literal)) {
            out.writeFloatNull();
        } else {
            out.writeFloat(((BigDecimal) literal.getValue()).floatValue());
        }
        return;

    case INTEGER:
        if (isLiteralNull(literal)) {
            out.writeIntNull();
        } else {
            out.writeInt((((BigDecimal) literal.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP)).intValue());
        }
        return;

    case DECIMAL:
        // Converting exact decimal into double since values in the list may have different scales
        // so the resulting scale wouldn't be calculated correctly
        if (isLiteralNull(literal)) {
            out.writeDoubleNull();
        } else {
            out.writeDouble(((BigDecimal) literal.getValue()).doubleValue());
        }
        return;

    case VARCHAR:
        if (isLiteralNull(literal)) {
            out.writeVarcharNull();
        } else {
            out.writeVarChar(((NlsString) literal.getValue()).getValue());
        }
        return;

    case SYMBOL:
        if (isLiteralNull(literal)) {
            out.writeVarcharNull();
        } else {
            out.writeVarChar(literal.getValue().toString());
        }
        return;

    case DATE:
        if (isLiteralNull(literal)) {
            out.writeDateNull();
        } else {
            out.writeDate(
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(new DateTime(literal.getValue()).getMillis()),
                            ZoneOffset.UTC).toLocalDate());
        }
        return;

    case TIME:
        if (isLiteralNull(literal)) {
            out.writeTimeNull();
        } else {
            out.writeTime(
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(new DateTime(literal.getValue()).getMillis()),
                            ZoneOffset.UTC).toLocalTime());
        }
        return;

    case TIMESTAMP:
        if (isLiteralNull(literal)) {
            out.writeTimestampNull();
        } else {
            out.writeTimestamp(LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(new DateTime(literal.getValue()).getMillis()), ZoneOffset.UTC));
        }
        return;

    case INTERVAL_YEAR:
    case INTERVAL_YEAR_MONTH:
    case INTERVAL_MONTH:
        if (isLiteralNull(literal)) {
            out.writeIntervalNull();
        } else {
            int months = ((BigDecimal) (literal.getValue())).intValue();
            out.writeInterval(new Period().plusMonths(months));
        }
        return;

    case INTERVAL_DAY:
    case INTERVAL_DAY_HOUR:
    case INTERVAL_DAY_MINUTE:
    case INTERVAL_DAY_SECOND:
    case INTERVAL_HOUR:
    case INTERVAL_HOUR_MINUTE:
    case INTERVAL_HOUR_SECOND:
    case INTERVAL_MINUTE:
    case INTERVAL_MINUTE_SECOND:
    case INTERVAL_SECOND:
        if (isLiteralNull(literal)) {
            out.writeIntervalNull();
        } else {
            long millis = ((BigDecimal) (literal.getValue())).longValue();
            int days = (int) (millis / DateTimeConstants.MILLIS_PER_DAY);
            millis = millis - (days * DateTimeConstants.MILLIS_PER_DAY);
            out.writeInterval(new Period().plusDays(days).plusMillis((int) millis));
        }
        return;

    case NULL:
        out.writeUntypedNull();
        return;

    case ANY:
    default:
        throw new UnsupportedOperationException(
                String.format("Unable to convert the value of %s and type %s to a Drill constant expression.",
                        literal, literal.getType().getSqlTypeName()));
    }
}

From source file:org.apache.drill.exec.planner.logical.DrillValuesRel.java

License:Apache License

private static void writeLiteral(RexLiteral literal, JsonOutput out) throws IOException {

    switch (literal.getType().getSqlTypeName()) {
    case BIGINT://from ww w.ja  v  a 2 s.c o  m
        if (isLiteralNull(literal)) {
            out.writeBigIntNull();
        } else {
            out.writeBigInt(
                    (((BigDecimal) literal.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP)).longValue());
        }
        return;

    case BOOLEAN:
        if (isLiteralNull(literal)) {
            out.writeBooleanNull();
        } else {
            out.writeBoolean((Boolean) literal.getValue());
        }
        return;

    case CHAR:
        if (isLiteralNull(literal)) {
            out.writeVarcharNull();
        } else {
            // Since Calcite treats string literals as fixed char and adds trailing spaces to the strings to make them the
            // same length, here we do an rtrim() to get the string without the trailing spaces. If we don't rtrim, the comparison
            // with Drill's varchar column values would not return a match.
            // TODO: However, note that if the user had explicitly added spaces in the string literals then even those would get
            // trimmed, so this exposes another issue that needs to be resolved.
            out.writeVarChar(((NlsString) literal.getValue()).rtrim().getValue());
        }
        return;

    case DOUBLE:
        if (isLiteralNull(literal)) {
            out.writeDoubleNull();
        } else {
            out.writeDouble(((BigDecimal) literal.getValue()).doubleValue());
        }
        return;

    case FLOAT:
        if (isLiteralNull(literal)) {
            out.writeFloatNull();
        } else {
            out.writeFloat(((BigDecimal) literal.getValue()).floatValue());
        }
        return;

    case INTEGER:
        if (isLiteralNull(literal)) {
            out.writeIntNull();
        } else {
            out.writeInt((((BigDecimal) literal.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP)).intValue());
        }
        return;

    case DECIMAL:
        if (isLiteralNull(literal)) {
            out.writeDoubleNull();
        } else {
            out.writeDouble(((BigDecimal) literal.getValue()).doubleValue());
        }
        logger.warn(
                "Converting exact decimal into approximate decimal.  Should be fixed once decimal is implemented.");
        return;

    case VARCHAR:
        if (isLiteralNull(literal)) {
            out.writeVarcharNull();
        } else {
            out.writeVarChar(((NlsString) literal.getValue()).getValue());
        }
        return;

    case SYMBOL:
        if (isLiteralNull(literal)) {
            out.writeVarcharNull();
        } else {
            out.writeVarChar(literal.getValue().toString());
        }
        return;

    case DATE:
        if (isLiteralNull(literal)) {
            out.writeDateNull();
        } else {
            out.writeDate(new DateTime((GregorianCalendar) literal.getValue()));
        }
        return;

    case TIME:
        if (isLiteralNull(literal)) {
            out.writeTimeNull();
        } else {
            out.writeTime(new DateTime((GregorianCalendar) literal.getValue()));
        }
        return;

    case TIMESTAMP:
        if (isLiteralNull(literal)) {
            out.writeTimestampNull();
        } else {
            out.writeTimestamp(new DateTime((GregorianCalendar) literal.getValue()));
        }
        return;

    case INTERVAL_YEAR_MONTH:
        if (isLiteralNull(literal)) {
            out.writeIntervalNull();
        } else {
            int months = ((BigDecimal) (literal.getValue())).intValue();
            out.writeInterval(new Period().plusMonths(months));
        }
        return;

    case INTERVAL_DAY_TIME:
        if (isLiteralNull(literal)) {
            out.writeIntervalNull();
        } else {
            long millis = ((BigDecimal) (literal.getValue())).longValue();
            int days = (int) (millis / MILLIS_IN_DAY);
            millis = millis - (days * MILLIS_IN_DAY);
            out.writeInterval(new Period().plusDays(days).plusMillis((int) millis));
        }
        return;

    case NULL:
        out.writeUntypedNull();
        return;

    case ANY:
    default:
        throw new UnsupportedOperationException(
                String.format("Unable to convert the value of %s and type %s to a Drill constant expression.",
                        literal, literal.getType().getSqlTypeName()));
    }
}