Example usage for org.apache.commons.lang.time DateUtils getFragmentInHours

List of usage examples for org.apache.commons.lang.time DateUtils getFragmentInHours

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils getFragmentInHours.

Prototype

public static long getFragmentInHours(Calendar calendar, int fragment) 

Source Link

Document

Returns the number of hours within the fragment.

Usage

From source file:net.audumla.climate.bom.BOMStatisticalClimateDataObserver.java

private WritableClimateObservation getObservation(WritableClimateData bomdata, int hour) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(0);/*from w w w.  j a  va  2s  . co m*/
    c.set(Calendar.HOUR_OF_DAY, hour);
    Date requiredTime = c.getTime();
    WritableClimateObservation obs = null;
    try {
        obs = ClimateDataFactory.convertToWritableClimateObservation(
                bomdata.getObservation(requiredTime, ClimateData.ObservationMatch.CLOSEST));
        if (obs != null && DateUtils.getFragmentInHours(obs.getTime(), Calendar.DAY_OF_YEAR) == hour) {
            return obs;
        }
    } catch (Exception ignored) {
        logger.error(ignored);
        ignored.printStackTrace();
    }
    obs = ClimateDataFactory.newWritableClimateObservation(this, getSource());
    obs.setTime(requiredTime);
    bomdata.addObservation(obs);
    return obs;
}

From source file:ch.algotrader.service.PortfolioServiceImpl.java

/**
 * {@inheritDoc}/*ww  w.j  a va  2  s.  c o m*/
 */
@Override
public Collection<PortfolioValueVO> getPortfolioValuesInclPerformanceSinceDate(final String strategyName,
        final Date date) {

    Validate.notEmpty(strategyName, "Strategy name is empty");
    Validate.notNull(date, "Date is null");

    Collection<PortfolioValueVO> portfolioValues = this.portfolioValueDao.findByStrategyAndMinDate(strategyName,
            date, PortfolioValueVOProducer.INSTANCE);

    // calculate the performance
    double lastNetLiqValue = 0;
    double lastDayNetLiqValue = 0;
    double performance = 1.0;
    double dailyPerformance = 1.0;
    for (PortfolioValueVO portfolioValue : portfolioValues) {

        // for AlgoTrader Server reset performance at the 24:00 based on NetLiqValue of prior day
        if (StrategyImpl.SERVER.equals(strategyName)
                && DateUtils.getFragmentInHours(portfolioValue.getDateTime(), Calendar.DAY_OF_YEAR) == 0) {
            if (lastDayNetLiqValue != 0) {
                dailyPerformance = dailyPerformance * (portfolioValue.getNetLiqValue().doubleValue()
                        / (lastDayNetLiqValue + (portfolioValue.getCashFlow() != null
                                ? portfolioValue.getCashFlow().doubleValue()
                                : 0)));
                performance = dailyPerformance;
                portfolioValue.setPerformance(performance - 1.0);
            }

            lastDayNetLiqValue = portfolioValue.getNetLiqValue().doubleValue();
            lastNetLiqValue = portfolioValue.getNetLiqValue().doubleValue();

        } else {
            if (lastNetLiqValue != 0) {
                performance = performance * (portfolioValue.getNetLiqValue().doubleValue() / (lastNetLiqValue
                        + (portfolioValue.getCashFlow() != null ? portfolioValue.getCashFlow().doubleValue()
                                : 0)));
                portfolioValue.setPerformance(performance - 1.0);
            }

            lastNetLiqValue = portfolioValue.getNetLiqValue().doubleValue();
        }
    }

    return portfolioValues;

}