Example usage for org.joda.time PeriodType days

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

Introduction

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

Prototype

public static PeriodType days() 

Source Link

Document

Gets a type that defines just the days field.

Usage

From source file:com.hmsinc.epicenter.util.DateTimeUtils.java

License:Open Source License

/**
  * Gets a difference in days, taking into account the timezone and DST.
  * /*from w ww. j a v  a 2  s . c  o  m*/
  * @param start
  * @param end
  * @return
  */
public static int deltaDays(final ReadableInstant start, final ReadableInstant end) {
    return new Period(start, end, PeriodType.days()).getDays();
}

From source file:com.huang.rp.common.utils.TimeUtils.java

License:Apache License

/**
 * ? //from   ww w  . j  a  v a  2s. co  m
 * @param dt1
 * @param dt2
 * @return
 */
public static int getDateDiff(DateTime dt1, DateTime dt2) {
    Period p = new Period(dt1, dt2, PeriodType.days());
    return p.getDays();
}

From source file:com.netflix.ice.basic.BasicDataManager.java

License:Apache License

private double[] getData(Interval interval, TagLists tagLists) throws ExecutionException {
    DateTime start = config.startDate;/*from w  ww .  j  av a2s . co m*/
    DateTime end = config.startDate;

    if (consolidateType == ConsolidateType.hourly) {
        start = interval.getStart().withDayOfMonth(1).withMillisOfDay(0);
        end = interval.getEnd();
    } else if (consolidateType == ConsolidateType.daily) {
        start = interval.getStart().withDayOfYear(1).withMillisOfDay(0);
        end = interval.getEnd();
    }

    int num = 0;
    if (consolidateType == ConsolidateType.hourly) {
        num = interval.toPeriod(PeriodType.hours()).getHours();
        if (interval.getStart().plusHours(num).isBefore(interval.getEnd()))
            num++;
    } else if (consolidateType == ConsolidateType.daily) {
        num = interval.toPeriod(PeriodType.days()).getDays();
        if (interval.getStart().plusDays(num).isBefore(interval.getEnd()))
            num++;
    } else if (consolidateType == ConsolidateType.weekly) {
        num = interval.toPeriod(PeriodType.weeks()).getWeeks();
        if (interval.getStart().plusWeeks(num).isBefore(interval.getEnd()))
            num++;
    } else if (consolidateType == ConsolidateType.monthly) {
        num = interval.toPeriod(PeriodType.months()).getMonths();
        if (interval.getStart().plusMonths(num).isBefore(interval.getEnd()))
            num++;
    }

    double[] result = new double[num];

    do {
        ReadOnlyData data = getReadOnlyData(start);

        int resultIndex = 0;
        int fromIndex = 0;

        if (interval.getStart().isBefore(start)) {
            if (consolidateType == ConsolidateType.hourly) {
                resultIndex = Hours.hoursBetween(interval.getStart(), start).getHours();
            } else if (consolidateType == ConsolidateType.daily) {
                resultIndex = Days.daysBetween(interval.getStart(), start).getDays();
            } else if (consolidateType == ConsolidateType.weekly) {
                resultIndex = Weeks.weeksBetween(interval.getStart(), start).getWeeks();
            } else if (consolidateType == ConsolidateType.monthly) {
                resultIndex = Months.monthsBetween(interval.getStart(), start).getMonths();
            }
        } else {
            if (consolidateType == ConsolidateType.hourly) {
                fromIndex = Hours.hoursBetween(start, interval.getStart()).getHours();
            } else if (consolidateType == ConsolidateType.daily) {
                fromIndex = Days.daysBetween(start, interval.getStart()).getDays();
            } else if (consolidateType == ConsolidateType.weekly) {
                fromIndex = Weeks.weeksBetween(start, interval.getStart()).getWeeks();
                if (start.getDayOfWeek() != interval.getStart().getDayOfWeek())
                    fromIndex++;
            } else if (consolidateType == ConsolidateType.monthly) {
                fromIndex = Months.monthsBetween(start, interval.getStart()).getMonths();
            }
        }

        List<Integer> columeIndexs = Lists.newArrayList();
        int columeIndex = 0;
        for (TagGroup tagGroup : data.getTagGroups()) {
            if (tagLists.contains(tagGroup))
                columeIndexs.add(columeIndex);
            columeIndex++;
        }
        while (resultIndex < num && fromIndex < data.getNum()) {
            double[] fromData = data.getData(fromIndex++);
            for (Integer cIndex : columeIndexs)
                result[resultIndex] += fromData[cIndex];
            resultIndex++;
        }

        if (consolidateType == ConsolidateType.hourly)
            start = start.plusMonths(1);
        else if (consolidateType == ConsolidateType.daily)
            start = start.plusYears(1);
        else
            break;
    } while (start.isBefore(end));

    return result;
}

From source file:com.squid.kraken.v4.core.analysis.engine.processor.AnalysisCompute.java

License:Open Source License

private IntervalleObject alignPastInterval(IntervalleObject presentInterval, IntervalleObject pastInterval,
        Axis joinAxis) throws ScopeException {

    if (joinAxis != null && presentInterval != null && pastInterval != null) {

        Object lowerPresent = presentInterval.getLowerBound();
        Object lowerPast = pastInterval.getLowerBound();
        Object upperPresent = presentInterval.getUpperBound();
        Object upperPast = pastInterval.getUpperBound();
        ////  w  ww .j  a  v  a  2  s. co  m
        IDomain image = joinAxis.getDefinition().getImageDomain();
        if (lowerPresent instanceof Date && lowerPast instanceof Date) {

            DateTime lowerPastDT = new DateTime((Date) lowerPast);
            DateTime lowerPresentDT = new DateTime((Date) lowerPresent);
            DateTime upperPresentDT = new DateTime((Date) upperPresent);
            DateTime upperPastDT = new DateTime((Date) upperPast);

            // realign
            if (image.isInstanceOf(IDomain.YEARLY)) {
                // check if present is an exact number of years
                if (lowerPresentDT.getDayOfYear() == 1
                        && upperPresentDT.getDayOfYear() == upperPresentDT.dayOfYear().getMaximumValue()) {
                    // check of both periods have the same number of days
                    Period presentPeriod = new Period(new LocalDate(lowerPresent),
                            (new LocalDate(upperPresent)), PeriodType.days());
                    Period pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)),
                            PeriodType.days());
                    if (presentPeriod.getDays() == pastPeriod.getDays()) {
                        presentPeriod = new Period(new LocalDate(lowerPresent),
                                (new LocalDate(upperPresent)).plusDays(1), PeriodType.years());
                        pastPeriod = new Period(new LocalDate(lowerPast),
                                (new LocalDate(upperPast)).plusDays(1), PeriodType.years());

                        // realign
                        if (presentPeriod.getYears() > pastPeriod.getYears()) {
                            // some days are missing to align the periods
                            if (lowerPastDT.getDayOfYear() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(upperPastDT.getYear(), 1, 1, 0, 0).toDate();
                                return new IntervalleObject(newLowerPast, upperPast);
                            }
                            if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) {
                                // year over year
                                Date newUpperPast = new DateTime(upperPastDT.getYear(), 12, 31, 23, 59)
                                        .toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);
                            }
                        } else {
                            // either already aligned, or some days should
                            // be removed

                            if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) {
                                // year over Year
                                Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59)
                                        .toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);

                            }
                            if (lowerPastDT.getDayOfYear() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0)
                                        .toDate();
                                return new IntervalleObject(newLowerPast, upperPast);
                            }

                        }
                    }
                }
            } else if (image.isInstanceOf(IDomain.QUATERLY) || image.isInstanceOf(IDomain.MONTHLY)) {
                // check if present is an exact number of month
                if (lowerPresentDT.getDayOfMonth() == 1
                        && upperPresentDT.getDayOfMonth() == upperPresentDT.dayOfMonth().getMaximumValue()) {
                    // check of both periods have the same number of days
                    Period presentPeriod = new Period(new LocalDate(lowerPresent), new LocalDate(upperPresent),
                            PeriodType.days());
                    Period pastPeriod = new Period(new LocalDate(lowerPast), new LocalDate(upperPast),
                            PeriodType.days());
                    if (presentPeriod.getDays() == pastPeriod.getDays()) {
                        // realign
                        presentPeriod = new Period(new LocalDate(lowerPresent),
                                (new LocalDate(upperPresent)).plusDays(1), PeriodType.months());
                        pastPeriod = new Period(new LocalDate(lowerPast),
                                (new LocalDate(upperPast)).plusDays(1), PeriodType.months());
                        if (presentPeriod.getMonths() > pastPeriod.getMonths()) {
                            // some days are missing

                            if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) {
                                // month over month
                                Date newUpperPast = new DateTime(upperPastDT.getYear(),
                                        upperPastDT.getMonthOfYear(),
                                        upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate();
                                return new IntervalleObject(lowerPast, newUpperPast);
                            }

                            if (lowerPastDT.getDayOfMonth() != 1) {
                                // previous period
                                Date newLowerPast = new DateTime(lowerPastDT.getYear(),
                                        lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate();
                                return new IntervalleObject(newLowerPast, upperPast);

                            }

                        } else {
                            // either already aligned, of some days should
                            // be removed
                            if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) {
                                /// month over month
                                if (upperPastDT.getMonthOfYear() == 1) {
                                    Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59)
                                            .toDate();
                                    return new IntervalleObject(lowerPast, newUpperPast);

                                } else {

                                    upperPastDT = upperPastDT.minusMonths(1);
                                    Date newUpperPast = new DateTime(upperPastDT.getYear(),
                                            upperPastDT.getMonthOfYear(),
                                            upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate();
                                    return new IntervalleObject(lowerPast, newUpperPast);
                                }
                            }
                            if (lowerPastDT.getDayOfMonth() != 1) {
                                // previous period
                                if (lowerPastDT.getMonthOfYear() == 12) {
                                    Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0)
                                            .toDate();
                                    return new IntervalleObject(newLowerPast, upperPast);

                                } else {
                                    lowerPastDT = lowerPastDT.plusMonths(1);
                                    Date newLowerPast = new DateTime(lowerPastDT.getYear(),
                                            lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate();
                                    return new IntervalleObject(newLowerPast, upperPast);

                                }

                            }

                        }
                    }
                }
            }
        }
    }
    return pastInterval;
}

From source file:com.squid.kraken.v4.core.analysis.engine.processor.AnalysisCompute.java

License:Open Source License

private PeriodType computePeriodType(IDomain image) {
    if (image.isInstanceOf(IDomain.YEARLY)) {
        return PeriodType.years();
    } else if (image.isInstanceOf(IDomain.QUATERLY)) {
        return PeriodType.months();
    } else if (image.isInstanceOf(IDomain.MONTHLY)) {
        return PeriodType.months();
    } else if (image.isInstanceOf(IDomain.WEEKLY)) {
        return PeriodType.weeks();
    } else {//from  ww w . j av a  2  s .c  om
        return PeriodType.days();
    }
}

From source file:com.webarch.common.datetime.DateTimeUtils.java

License:Apache License

/**
 * ?//  w  w w .  ja va 2  s.  c o m
 *
 * @param startTime
 * @param endTime
 * @return
 */
public static long getPeriodDays(Date startTime, Date endTime) {
    return getTimePeriod(startTime, endTime, PeriodType.days()).getDays();
}

From source file:fm.last.commons.lang.time.DateRange.java

License:Apache License

/**
 * @return The number of days (inclusive) between the first and last dates in the range
 *//* w  ww. j ava2  s . c  o m*/
public int numberOfDays() {
    Period period = new Period(firstDate, lastDate, PeriodType.days());
    return period.getDays() + 1; // Add one to be inclusive of lastDate as well
}

From source file:info.culebrasgis.calculadoradefechas.DiasDesdeActivity.java

License:Open Source License

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dias_desde);

    fechaUsuario = (Button) findViewById(R.id.buttonFechaUsuario);
    fechaUsuario.setOnClickListener(new View.OnClickListener() {
        @Override//from   ww  w.  ja  v a2 s . c  o  m
        public void onClick(View v) {
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getSupportFragmentManager(), "datePicker");
        }
    });

    resultado = (TextView) findViewById(R.id.textViewResultado);

    volver = (Button) findViewById(R.id.buttonVolver);
    volver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    reiniciar = (Button) findViewById(R.id.buttonReiniciar);
    reiniciar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resultado.setText(R.string.resultado);
            fechaUsuario.setText(R.string.fecha);
        }
    });

    calcular = (Button) findViewById(R.id.buttonCalcular);
    calcular.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                int dia, mes, anno;
                dia = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(0, 2));
                mes = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(3, 5));
                anno = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(6, 10));

                DateTime hoy = new DateTime();
                DateTime fecha = new DateTime(anno, mes, dia, 0, 0);

                if (fecha.isAfter(hoy)) { // si el usuario mete una fecha posterior al da actual
                    resultado.setText(R.string.fecha_anterior);
                } else {
                    Period periodo = new Period(fecha, hoy, PeriodType.days());
                    resultado.setText(
                            String.format(getString(R.string.resultado_dias_desde), periodo.getDays()));
                }
            } catch (Exception e) {
                resultado.setText(R.string.fecha_incorrecta);
            }
        }
    });

}

From source file:info.culebrasgis.calculadoradefechas.DiasHastaActivity.java

License:Open Source License

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dias_hasta);

    fechaUsuario = (Button) findViewById(R.id.buttonFechaUsuario);
    fechaUsuario.setOnClickListener(new View.OnClickListener() {
        @Override//from   w w w  . jav  a2 s  .c  o m
        public void onClick(View v) {
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getSupportFragmentManager(), "datePicker");
        }
    });

    resultado = (TextView) findViewById(R.id.textViewResultado);

    volver = (Button) findViewById(R.id.buttonVolver);
    volver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    reiniciar = (Button) findViewById(R.id.buttonReiniciar);
    reiniciar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resultado.setText(R.string.resultado);
            fechaUsuario.setText(R.string.fecha);
        }
    });

    calcular = (Button) findViewById(R.id.buttonCalcular);
    calcular.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                int dia, mes, anno;
                dia = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(0, 2));
                mes = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(3, 5));
                anno = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(6, 10));

                DateTime hoy = new DateTime();
                DateTime fecha = new DateTime(anno, mes, dia, 0, 0);

                if (fecha.isBefore(hoy)) { // si el usuario mete una fecha posterior al da actual
                    resultado.setText(R.string.fecha_posterior);
                } else {
                    Period periodo = new Period(hoy, fecha, PeriodType.days());
                    resultado.setText(
                            String.format(getString(R.string.resultado_dias_hasta), (periodo.getDays() + 1)));
                }
            } catch (Exception e) {
                resultado.setText(R.string.fecha_incorrecta);
            }
        }
    });

}

From source file:net.objectlab.kit.datecalc.joda.LocalDatePeriodCountCalculator.java

License:Apache License

public int dayDiff(final LocalDate start, final LocalDate end, final PeriodCountBasis basis) {
    int diff = 0;

    switch (basis) {
    case CONV_30_360:
        diff = diffConv30v360(start, end);
        break;//from  w w w.j  a  v a 2s  .  co m

    case CONV_360E_ISDA:
        diff = diff360EIsda(start, end);
        break;

    case CONV_360E_ISMA:
        diff = diff360EIsma(start, end);
        break;
    default:
        final Period p = new Period(start, end, PeriodType.days());
        diff = p.getDays();
    }

    return diff;
}