Example usage for org.joda.time Period days

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

Introduction

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

Prototype

public static Period days(int days) 

Source Link

Document

Create a period with a specified number of days.

Usage

From source file:com.billing.ng.entities.BillingPeriod.java

License:Open Source License

/**
 * Returns a Joda Time <code>Period</code> representing the period of time
 * between the start of any given cycle and the Nth cycle (give as cycleNumber).
 *
 * @param cycleNumber cycle number// ww  w.  j a  va2  s.  co  m
 * @return period of time
 */
public Period getPeriodOfTime(Integer cycleNumber) {
    Integer interval = cycleNumber * getInterval();

    switch (getType()) {
    case DAY:
        return Period.days(interval);
    case WEEK:
        return Period.weeks(interval);
    case MONTH:
        return Period.months(interval);
    case YEAR:
        return Period.years(interval);
    }
    return null;
}

From source file:com.ctp.util.CsvTicksLoader.java

License:Open Source License

public static void main(String args[]) {
    /* TimeSeries series = CsvTicksLoader.loadAppleIncSeries();
            // www. j  ava  2s . co m
     System.out.println("Series: " + series.getName() + " (" + series.getSeriesPeriodDescription() + ")");
     System.out.println("Number of ticks: " + series.getTickCount());
     System.out.println("First tick: \n"
        + "\tVolume: " + series.getTick(0).getVolume() + "\n"
        + "\tOpen price: " + series.getTick(0).getOpenPrice()+ "\n"
        + "\tClose price: " + series.getTick(0).getClosePrice());*/
    CsvTicksLoader.writeAppleIncSeries(new Tick(Period.days(1), new DateTime(), Decimal.valueOf(1),
            Decimal.valueOf(2), Decimal.valueOf(3), Decimal.valueOf(5), Decimal.valueOf(5)));
}

From source file:com.netflix.iep.config.Strings.java

License:Apache License

private static Period parseAtPeriod(String amt, String unit) {
    int v = Integer.valueOf(amt);
    if (unit.equals("s") || unit.equals("second") || unit.equals("seconds"))
        return Period.seconds(v);
    if (unit.equals("m") || unit.equals("min") || unit.equals("minute") || unit.equals("minutes"))
        return Period.minutes(v);
    if (unit.equals("h") || unit.equals("hour") || unit.equals("hours"))
        return Period.hours(v);
    if (unit.equals("d") || unit.equals("day") || unit.equals("days"))
        return Period.days(v);
    if (unit.equals("w") || unit.equals("wk") || unit.equals("week") || unit.equals("weeks"))
        return Period.weeks(v);
    if (unit.equals("month") || unit.equals("months"))
        return Period.months(v);
    if (unit.equals("y") || unit.equals("year") || unit.equals("years"))
        return Period.years(v);
    throw new IllegalArgumentException("unknown unit " + unit);
}

From source file:com.quant.Tick.java

License:Open Source License

/**
 * Constructor.//from w  ww .j  a  v  a 2  s. c o  m
 * @param endTime the end time of the tick period
 * @param openPrice the open price of the tick period
 * @param highPrice the highest price of the tick period
 * @param lowPrice the lowest price of the tick period
 * @param closePrice the close price of the tick period
 * @param volume the volume of the tick period
 */
public Tick(DateTime endTime, Decimal openPrice, Decimal highPrice, Decimal lowPrice, Decimal closePrice,
        Decimal volume) {
    this(Period.days(1), endTime, openPrice, highPrice, lowPrice, closePrice, volume);
}

From source file:com.quant.TimeSeries.java

License:Open Source License

/**
 * Computes the time period of the series.
 *//*w ww  .ja  v a  2s .  co m*/
private void computeTimePeriod() {

    Period minPeriod = null;
    for (int i = beginIndex; i < endIndex; i++) {
        // For each tick interval...
        // Looking for the minimum period.
        long currentPeriodMillis = getTick(i + 1).getEndTime().getMillis()
                - getTick(i).getEndTime().getMillis();
        if (minPeriod == null) {
            minPeriod = new Period(currentPeriodMillis);
        } else {
            long minPeriodMillis = minPeriod.getMillis();
            if (minPeriodMillis > currentPeriodMillis) {
                minPeriod = new Period(currentPeriodMillis);
            }
        }
    }
    if (minPeriod == null || Period.ZERO.equals(minPeriod)) {
        // Minimum period not found (or zero ms found)
        // --> Use a one-day period
        minPeriod = Period.days(1);
    }
    timePeriod = minPeriod;
}

From source file:com.serotonin.m2m2.Common.java

License:Open Source License

public static Period getPeriod(int periodType, int periods) {
    switch (periodType) {
    case TimePeriods.MILLISECONDS:
        return Period.millis(periods);
    case TimePeriods.SECONDS:
        return Period.seconds(periods);
    case TimePeriods.MINUTES:
        return Period.minutes(periods);
    case TimePeriods.HOURS:
        return Period.hours(periods);
    case TimePeriods.DAYS:
        return Period.days(periods);
    case TimePeriods.WEEKS:
        return Period.weeks(periods);
    case TimePeriods.MONTHS:
        return Period.months(periods);
    case TimePeriods.YEARS:
        return Period.years(periods);
    default://from   ww w .  j  a va  2s .  c  om
        throw new ShouldNeverHappenException("Unsupported time period: " + periodType);
    }
}

From source file:com.thinkbiganalytics.scheduler.util.TimerToCronExpression.java

License:Apache License

/**
 * Parse a timer string to a Joda time period
 *
 * @param timer      a string indicating a time unit (i.e. 5 sec)
 * @param periodType the Period unit to use.
 *///from   w w w.  j av a2s . c o m
public static Period timerStringToPeriod(String timer, PeriodType periodType) {
    String cronString = null;
    Integer time = Integer.parseInt(StringUtils.substringBefore(timer, " "));
    String units = StringUtils.substringAfter(timer, " ").toLowerCase();
    //time to years,days,months,hours,min, sec
    Integer days = 0;
    Integer hours = 0;
    Integer min = 0;
    Integer sec = 0;
    Period p = null;
    if (units.startsWith("sec")) {
        p = Period.seconds(time);
    } else if (units.startsWith("min")) {
        p = Period.minutes(time);
    } else if (units.startsWith("hr") || units.startsWith("hour")) {
        p = Period.hours(time);
    } else if (units.startsWith("day")) {
        p = Period.days(time);
    }
    if (periodType != null) {
        p = p.normalizedStandard(periodType);
    } else {
    }
    return p;
}

From source file:de.ifgi.airbase.feeder.data.EEAMeasurementType.java

License:Open Source License

public Period getPeriod() {
    switch (this) {
    case HOUR:/*w ww  .  j  a  va  2s.  c o  m*/
        return Period.hours(1);
    case THREE_HOURS:
        return Period.hours(3);
    case EIGHT_HOURS:
        return Period.hours(8);
    case DAY:
    case DAY_MAX:
        return Period.days(1);
    case WEEK:
        return Period.weeks(1);
    case TWO_WEEKS:
        return Period.weeks(2);
    case FOUR_WEEKS:
        return Period.weeks(4);
    case MONTH:
        return Period.months(1);
    case THREE_MONTH:
        return Period.months(3);
    case YEAR:
        return Period.years(1);
    case VAR:
    default:
        throw new Error("Period not known");
    }
}

From source file:de.ifgi.airbase.feeder.io.sos.http.AbstractTransactionalSosClient.java

License:Open Source License

protected int getLastSundayOfOctober(int year) {
    LocalDate ld = new LocalDate(year, DateTimeConstants.OCTOBER, 1).dayOfMonth().withMaximumValue().dayOfWeek()
            .setCopy(DateTimeConstants.SUNDAY);
    if (ld.getMonthOfYear() != DateTimeConstants.OCTOBER) {
        return ld.minus(Period.days(7)).getDayOfMonth();
    } else {//from ww  w  .  j av  a 2  s .  c om
        return ld.getDayOfMonth();
    }
}

From source file:io.druid.sql.calcite.expression.builtin.CastOperatorConversion.java

License:Apache License

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature,
        final RexNode rexNode) {
    final RexNode operand = ((RexCall) rexNode).getOperands().get(0);
    final DruidExpression operandExpression = Expressions.toDruidExpression(plannerContext, rowSignature,
            operand);//  w  ww.j a  va2  s  .  c  om

    if (operandExpression == null) {
        return null;
    }

    final SqlTypeName fromType = operand.getType().getSqlTypeName();
    final SqlTypeName toType = rexNode.getType().getSqlTypeName();

    if (SqlTypeName.CHAR_TYPES.contains(fromType) && SqlTypeName.DATETIME_TYPES.contains(toType)) {
        return castCharToDateTime(plannerContext, operandExpression, toType);
    } else if (SqlTypeName.DATETIME_TYPES.contains(fromType) && SqlTypeName.CHAR_TYPES.contains(toType)) {
        return castDateTimeToChar(plannerContext, operandExpression, fromType);
    } else {
        // Handle other casts.
        final ExprType fromExprType = EXPRESSION_TYPES.get(fromType);
        final ExprType toExprType = EXPRESSION_TYPES.get(toType);

        if (fromExprType == null || toExprType == null) {
            // We have no runtime type for these SQL types.
            return null;
        }

        final DruidExpression typeCastExpression;

        if (fromExprType != toExprType) {
            // Ignore casts for simple extractions (use Function.identity) since it is ok in many cases.
            typeCastExpression = operandExpression.map(Function.identity(),
                    expression -> StringUtils.format("CAST(%s, '%s')", expression, toExprType.toString()));
        } else {
            typeCastExpression = operandExpression;
        }

        if (toType == SqlTypeName.DATE) {
            // Floor to day when casting to DATE.
            return TimeFloorOperatorConversion.applyTimestampFloor(typeCastExpression,
                    new PeriodGranularity(Period.days(1), null, plannerContext.getTimeZone()),
                    plannerContext.getExprMacroTable());
        } else {
            return typeCastExpression;
        }
    }
}