Example usage for org.joda.time DateTime getDayOfYear

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

Introduction

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

Prototype

public int getDayOfYear() 

Source Link

Document

Get the day of year field value.

Usage

From source file:org.springframework.analytics.metrics.memory.InMemoryAggregateCounter.java

License:Apache License

public AggregateCounter getCounts(Interval interval, AggregateCounterResolution resolution) {
    DateTime start = interval.getStart();
    DateTime end = interval.getEnd();//from  w w  w . j a  v  a 2s.c o m
    Chronology c = interval.getChronology();

    long[] counts;
    if (resolution == AggregateCounterResolution.minute) {
        List<long[]> days = accumulateDayCounts(minuteCountsByDay, start, end, 60 * 24);

        counts = MetricUtils.concatArrays(days, interval.getStart().getMinuteOfDay(),
                interval.toPeriod().toStandardMinutes().getMinutes() + 1);
    } else if (resolution == AggregateCounterResolution.hour) {
        List<long[]> days = accumulateDayCounts(hourCountsByDay, start, end, 24);

        counts = MetricUtils.concatArrays(days, interval.getStart().getHourOfDay(),
                interval.toPeriod().toStandardHours().getHours() + 1);
    } else if (resolution == AggregateCounterResolution.day) {
        DateTime startDay = new DateTime(c.dayOfYear().roundFloor(start.getMillis()));
        DateTime endDay = new DateTime(c.dayOfYear().roundFloor(end.plusDays(1).getMillis()));
        int nDays = Days.daysBetween(startDay, endDay).getDays();
        DateTime cursor = new DateTime(c.year().roundFloor(interval.getStartMillis()));
        List<long[]> yearDays = new ArrayList<long[]>();
        DateTime endYear = new DateTime(c.year().roundCeiling(end.getMillis()));

        while (cursor.isBefore(endYear)) {
            long[] dayCounts = dayCountsByYear.get(cursor.getYear());
            if (dayCounts == null) {
                // Querying where we have no data
                dayCounts = new long[daysInYear(cursor.getYear())];
            }
            yearDays.add(dayCounts);
            cursor = cursor.plusYears(1);
        }

        counts = MetricUtils.concatArrays(yearDays, startDay.getDayOfYear() - 1, nDays);

    } else if (resolution == AggregateCounterResolution.month) {
        DateTime startMonth = new DateTime(c.monthOfYear().roundFloor(interval.getStartMillis()));
        DateTime endMonth = new DateTime(c.monthOfYear().roundFloor(end.plusMonths(1).getMillis()));
        int nMonths = Months.monthsBetween(startMonth, endMonth).getMonths();
        DateTime cursor = new DateTime(c.year().roundFloor(interval.getStartMillis()));
        List<long[]> yearMonths = new ArrayList<long[]>();
        DateTime endYear = new DateTime(c.year().roundCeiling(end.getMillis()));

        while (cursor.isBefore(endYear)) {
            long[] monthCounts = monthCountsByYear.get(cursor.getYear());
            if (monthCounts == null) {
                monthCounts = new long[12];
            }
            yearMonths.add(monthCounts);
            cursor = cursor.plusYears(1);
        }

        counts = MetricUtils.concatArrays(yearMonths, startMonth.getMonthOfYear() - 1, nMonths);
    } else if (resolution == AggregateCounterResolution.year) {
        DateTime startYear = new DateTime(interval.getStart().getYear(), 1, 1, 0, 0);
        DateTime endYear = new DateTime(end.getYear() + 1, 1, 1, 0, 0);
        int nYears = Years.yearsBetween(startYear, endYear).getYears();
        counts = new long[nYears];

        for (int i = 0; i < nYears; i++) {
            long[] monthCounts = monthCountsByYear.get(startYear.plusYears(i).getYear());
            counts[i] = MetricUtils.sum(monthCounts);
        }

    } else {
        throw new IllegalStateException("Shouldn't happen. Unhandled resolution: " + resolution);
    }
    return new AggregateCounter(this.name, interval, counts, resolution);
}

From source file:org.springframework.analytics.metrics.memory.InMemoryAggregateCounter.java

License:Apache License

synchronized long increment(long amount, DateTime dateTime) {
    int year = dateTime.getYear();
    int month = dateTime.getMonthOfYear();
    int day = dateTime.getDayOfYear();
    int hour = dateTime.getHourOfDay();
    int minute = dateTime.getMinuteOfDay();

    long[] monthCounts = monthCountsByYear.get(year);
    long[] dayCounts = dayCountsByYear.get(year);

    if (monthCounts == null) {
        monthCounts = new long[12];
        monthCountsByYear.put(year, monthCounts);
        dayCounts = new long[daysInYear(year)];
        dayCountsByYear.put(year, dayCounts);
    }/*from w  w w . java 2  s .  c  o  m*/

    int countsByDayKey = year * 1000 + day;
    long[] hourCounts = hourCountsByDay.get(countsByDayKey);

    if (hourCounts == null) {
        hourCounts = new long[24];
        hourCountsByDay.put(countsByDayKey, hourCounts);
    }

    long[] minuteCounts = minuteCountsByDay.get(countsByDayKey);

    if (minuteCounts == null) {
        minuteCounts = new long[60 * 24];
        minuteCountsByDay.put(countsByDayKey, minuteCounts);
    }

    minuteCounts[minute] += amount;
    monthCounts[month - 1] += amount;
    dayCounts[day - 1] += amount;
    hourCounts[hour] += amount;

    return increment(amount);
}

From source file:org.springframework.xd.analytics.metrics.memory.InMemoryAggregateCounter.java

License:Apache License

public AggregateCount getCounts(Interval interval, AggregateCountResolution resolution) {
    DateTime start = interval.getStart();
    DateTime end = interval.getEnd();/*from  w  w w. j  a v  a2 s . co m*/
    Chronology c = interval.getChronology();

    long[] counts;
    if (resolution == AggregateCountResolution.minute) {
        List<long[]> days = accumulateDayCounts(minuteCountsByDay, start, end, 60 * 24);

        counts = MetricUtils.concatArrays(days, interval.getStart().getMinuteOfDay(),
                interval.toPeriod().toStandardMinutes().getMinutes() + 1);
    } else if (resolution == AggregateCountResolution.hour) {
        List<long[]> days = accumulateDayCounts(hourCountsByDay, start, end, 24);

        counts = MetricUtils.concatArrays(days, interval.getStart().getHourOfDay(),
                interval.toPeriod().toStandardHours().getHours() + 1);
    } else if (resolution == AggregateCountResolution.day) {
        DateTime startDay = new DateTime(c.dayOfYear().roundFloor(start.getMillis()));
        DateTime endDay = new DateTime(c.dayOfYear().roundFloor(end.plusDays(1).getMillis()));
        int nDays = Days.daysBetween(startDay, endDay).getDays();
        DateTime cursor = new DateTime(c.year().roundFloor(interval.getStartMillis()));
        List<long[]> yearDays = new ArrayList<long[]>();
        DateTime endYear = new DateTime(c.year().roundCeiling(end.getMillis()));

        while (cursor.isBefore(endYear)) {
            long[] dayCounts = dayCountsByYear.get(cursor.getYear());
            if (dayCounts == null) {
                // Querying where we have no data
                dayCounts = new long[daysInYear(cursor.getYear())];
            }
            yearDays.add(dayCounts);
            cursor = cursor.plusYears(1);
        }

        counts = MetricUtils.concatArrays(yearDays, startDay.getDayOfYear() - 1, nDays);

    } else if (resolution == AggregateCountResolution.month) {
        DateTime startMonth = new DateTime(c.monthOfYear().roundFloor(interval.getStartMillis()));
        DateTime endMonth = new DateTime(c.monthOfYear().roundFloor(end.plusMonths(1).getMillis()));
        int nMonths = Months.monthsBetween(startMonth, endMonth).getMonths();
        DateTime cursor = new DateTime(c.year().roundFloor(interval.getStartMillis()));
        List<long[]> yearMonths = new ArrayList<long[]>();
        DateTime endYear = new DateTime(c.year().roundCeiling(end.getMillis()));

        while (cursor.isBefore(endYear)) {
            long[] monthCounts = monthCountsByYear.get(cursor.getYear());
            if (monthCounts == null) {
                monthCounts = new long[12];
            }
            yearMonths.add(monthCounts);
            cursor = cursor.plusYears(1);
        }

        counts = MetricUtils.concatArrays(yearMonths, startMonth.getMonthOfYear() - 1, nMonths);
    } else if (resolution == AggregateCountResolution.year) {
        DateTime startYear = new DateTime(interval.getStart().getYear(), 1, 1, 0, 0);
        DateTime endYear = new DateTime(end.getYear() + 1, 1, 1, 0, 0);
        int nYears = Years.yearsBetween(startYear, endYear).getYears();
        counts = new long[nYears];

        for (int i = 0; i < nYears; i++) {
            long[] monthCounts = monthCountsByYear.get(startYear.plusYears(i).getYear());
            counts[i] = MetricUtils.sum(monthCounts);
        }

    } else {
        throw new IllegalStateException("Shouldn't happen. Unhandled resolution: " + resolution);
    }
    return new AggregateCount(getName(), interval, counts, resolution);
}

From source file:org.tanrabad.survey.utils.time.DaysAgoPrinter.java

License:Apache License

@Override
public String print(long referenceTime) {
    DateTime currentTimeInMills = new DateTime(currentTimer.getInMills());
    DateTime agoDateTime = new DateTime(referenceTime);

    if (currentTimeInMills.getDayOfYear() - agoDateTime.getDayOfYear() == 1) {
        DateTime dateTime = new DateTime(referenceTime);
        return String.format(Locale.US, " %02d:%02d", dateTime.getHourOfDay(),
                dateTime.getMinuteOfHour());
    }/* w  ww .j a  va 2  s  .com*/
    return null;
}

From source file:org.tanrabad.survey.utils.time.TimePrettyPrinterFactory.java

License:Apache License

@Override
public String print(long referenceTime) {
    DateTime reference = new DateTime(referenceTime);
    DateTime current = new DateTime(currentTimer.getInMills());

    long diffMills = current.getMillis() - reference.getMillis();
    if (diffMills < MINITE_IN_MILLS)
        return new SecondsAgoPrinter(currentTimer).print(referenceTime);
    else if (diffMills < HOUR_IN_MILLS)
        return new MinuteAgoPrinter(currentTimer).print(referenceTime);
    else if (current.getYear() == reference.getYear() && current.getDayOfYear() - reference.getDayOfYear() == 0)
        return new HoursAgoPrinter(currentTimer).print(referenceTime);
    else if (current.getYear() == reference.getYear() && current.getDayOfYear() - reference.getDayOfYear() == 1)
        return new DaysAgoPrinter(currentTimer).print(referenceTime);
    else/*  w w w  .  ja va2 s  .  co  m*/
        return new DateTimePrinter(currentTimer).print(referenceTime);

}

From source file:playground.johannes.gsv.synPop.invermo.InfereVacationsType.java

License:Open Source License

@Override
public void apply(ProxyPlan plan) {
    boolean hasVacations = false;
    for (ProxyObject act : plan.getActivities()) {
        if ("vacations".equalsIgnoreCase(act.getAttribute(CommonKeys.ACTIVITY_TYPE))) {
            hasVacations = true;//w  w w .j  a v  a  2 s .co  m
            break;
        }
    }

    if (hasVacations) {
        boolean isLong = false;

        ProxyObject first = plan.getLegs().get(0);
        ProxyObject last = plan.getLegs().get(plan.getLegs().size() - 1);

        String startStr = first.getAttribute(CommonKeys.LEG_START_TIME);
        String endStr = last.getAttribute(CommonKeys.LEG_END_TIME);

        if (startStr != null && endStr != null) {
            DateTime start = SplitPlanTask.formatter.parseDateTime(startStr);
            DateTime end = SplitPlanTask.formatter.parseDateTime(endStr);

            if (end.getDayOfYear() - start.getDayOfYear() > 3) {
                isLong = true;
            }
        }

        for (ProxyObject act : plan.getActivities()) {
            if ("vacations".equalsIgnoreCase(act.getAttribute(CommonKeys.ACTIVITY_TYPE))) {
                if (isLong) {
                    act.setAttribute(CommonKeys.ACTIVITY_TYPE, "vacations_long");
                } else {
                    act.setAttribute(CommonKeys.ACTIVITY_TYPE, "vacations_short");
                }
            }
        }

    }
}

From source file:snowMeltingPointCaseOLD.SimpleEIFactory.java

License:GNU General Public License

public static EnergyIndex createModel(boolean doHourly, DateTime date, double latitude, int i, int j,
        WritableRaster energyIJanuary, WritableRaster energyIFebruary, WritableRaster energyIMarch,
        WritableRaster energyIApril, WritableRaster energyIMay, WritableRaster energyIJune) {

    EnergyIndex model = null;/*  ww  w. j a v a 2  s  .  c  om*/

    int month = date.getMonthOfYear();
    int hour = date.getHourOfDay();
    int day = date.getDayOfYear();

    double dayangb = Math.toRadians((360 / 365.25) * (day - 79.436));

    // Evaluate the declination of the sun.
    double delta = .3723 + 23.2567 * Math.sin(dayangb) - .758 * Math.cos(dayangb)
            + .1149 * Math.sin(2 * dayangb) + .3656 * Math.cos(2 * dayangb) - .1712 * Math.sin(3 * dayangb)
            + .0201 * Math.cos(3 * dayangb);
    // Evaluate the radiation in this day.
    double ss = Math.acos(-Math.tan(Math.toRadians(delta)) * Math.tan(latitude));

    double sunrise = 12 * (1.0 - ss / Math.PI);
    double sunset = 12 * (1.0 + ss / Math.PI);

    String EIvalue;

    if ((hour >= (sunrise) && hour <= (sunset)) | doHourly == false)
        EIvalue = "daily";
    else
        EIvalue = "minimum";

    if (EIvalue.equals("daily") & month == 1) {
        model = new EIdaily(energyIJanuary, i, j, daysToJan);

    } else if (EIvalue.equals("daily") & month == 2) {
        model = new EIdaily(energyIFebruary, i, j, daysToFeb);

    } else if (EIvalue.equals("daily") & month == 3) {
        model = new EIdaily(energyIMarch, i, j, daysToMar);

    } else if (EIvalue.equals("daily") & month == 4) {
        model = new EIdaily(energyIApril, i, j, daysToApr);

    } else if (EIvalue.equals("daily") & month == 5) {
        model = new EIdaily(energyIMay, i, j, daysToMay);

    } else if (EIvalue.equals("daily") & month == 6) {
        model = new EIdaily(energyIJune, i, j, daysToJune);

    } else if (EIvalue.equals("daily") & month == 7) {
        model = new EIdaily(energyIJune, i, j, daysToJune);

    } else if (EIvalue.equals("daily") & month == 8) {
        model = new EIdaily(energyIJune, i, j, daysToJune);

    } else if (EIvalue.equals("daily") & month == 9) {

        model = new EIdaily(energyIJune, i, j, daysToJune);

    } else if (EIvalue.equals("daily") & month == 10) {
        model = new EIdaily(energyIFebruary, i, j, daysToFeb);

    } else if (EIvalue.equals("daily") & month == 11) {
        model = new EIdaily(energyIFebruary, i, j, daysToFeb);

    } else if (EIvalue.equals("daily") & month == 12) {
        model = new EIdaily(energyIFebruary, i, j, daysToFeb);

    } else if (EIvalue.equals("mimimum") & month == 1) {
        model = new EIminimum(energyIJanuary, daysToJan);

    } else if (EIvalue.equals("minimum") & month == 2) {
        model = new EIminimum(energyIFebruary, daysToFeb);

    } else if (EIvalue.equals("minimum") & month == 3) {
        model = new EIminimum(energyIMarch, daysToMar);

    } else if (EIvalue.equals("minimum") & month == 4) {
        model = new EIminimum(energyIApril, daysToApr);

    } else if (EIvalue.equals("minimum") & month == 5) {
        model = new EIminimum(energyIMay, daysToMay);

    } else if (EIvalue.equals("minimum") & month == 6) {
        model = new EIminimum(energyIJune, daysToJune);

    } else if (EIvalue.equals("minimum") & month == 7) {
        model = new EIminimum(energyIJune, daysToJune);

    } else if (EIvalue.equals("minimum") & month == 8) {
        model = new EIminimum(energyIJune, daysToJune);

    } else if (EIvalue.equals("minimum") & month == 9) {

        model = new EIminimum(energyIJune, daysToJune);

    } else if (EIvalue.equals("minimum") & month == 10) {
        model = new EIminimum(energyIFebruary, daysToFeb);

    } else if (EIvalue.equals("minimum") & month == 11) {
        model = new EIminimum(energyIFebruary, daysToFeb);

    } else if (EIvalue.equals("minimum") & month == 12) {
        model = new EIminimum(energyIFebruary, daysToFeb);
    }

    return model;

}

From source file:swrbPointCase.ShortwaveRadiationBalancePointCase.java

License:GNU General Public License

/**
 * getHourAngle is the value of the hour angle at a given time and latitude (Corripio (2003))
 * /*from   w  w w .j av a 2s.  co m*/
 *
 * @param date is the current date
 * @param latitude is the latitude of the station
 * @return the double value of the hour angle
 */
private double getHourAngle(DateTime date, double latitude) {
    int day = date.getDayOfYear();

    hour = (doHourly == true) ? (double) date.getMillisOfDay() / (1000 * 60 * 60) : 12.5;

    // (360 / 365.25) * (day - 79.436) is the number of the day 
    double dayangb = Math.toRadians((360 / 365.25) * (day - 79.436));

    // Evaluate the declination of the sun.
    delta = Math.toRadians(.3723 + 23.2567 * Math.sin(dayangb) - .758 * Math.cos(dayangb)
            + .1149 * Math.sin(2 * dayangb) + .3656 * Math.cos(2 * dayangb) - .1712 * Math.sin(3 * dayangb)
            + .0201 * Math.cos(3 * dayangb));

    // ss is the absolute value of the hour angle at sunrise or sunset      
    double ss = Math.acos(-Math.tan(delta) * Math.tan(latitude));
    sunrise = 12 * (1.0 - ss / Math.PI);
    sunset = 12 * (1.0 + ss / Math.PI);

    if (hour > (sunrise) && hour < (sunset) & (hour - sunrise) < 0.01)
        hour = hour + 0.1;
    if (hour > (sunrise) && hour < (sunset) & (sunset - hour) < 0.01)
        hour = hour - 0.1;

    //the hour angle is zero at noon and has the following value in radians at any time t
    // given in hours and decimal fraction:
    double hourangle = (hour / 12.0 - 1.0) * Math.PI;
    return hourangle;

}

From source file:swrbRasterCase.ShortwaveRadiationBalanceRasterCase.java

License:Open Source License

/**
 * getHourAngle is the value of the hour angle at a given time and latitude (Corripio (2003))
 * //w w w  .  ja  v a2s.co m
 *
 * @param date is the current date
 * @param latitude is the latitude of the station
 * @return the double value of the hour angle
 */
private double getHourAngle(DateTime date, double latitude) {
    int day = date.getDayOfYear();
    hour = (doHourly == true) ? (double) date.getMillisOfDay() / (1000 * 60 * 60) : 12.5;

    // (360 / 365.25) * (day - 79.436) is the number of the day 
    double dayangb = Math.toRadians((360 / 365.25) * (day - 79.436));

    // Evaluate the declination of the sun.
    delta = Math.toRadians(.3723 + 23.2567 * Math.sin(dayangb) - .758 * Math.cos(dayangb)
            + .1149 * Math.sin(2 * dayangb) + .3656 * Math.cos(2 * dayangb) - .1712 * Math.sin(3 * dayangb)
            + .0201 * Math.cos(3 * dayangb));

    // ss is the absolute value of the hour angle at sunrise or sunset
    double ss = Math.acos(-Math.tan(delta) * Math.tan(latitude));
    sunrise = 12 * (1.0 - ss / Math.PI);
    sunset = 12 * (1.0 + ss / Math.PI);

    if (hour > (sunrise) && hour < (sunset) & (hour - sunrise) < 0.01)
        hour = hour + 0.1;
    if (hour > (sunrise) && hour < (sunset) & (sunset - hour) < 0.01)
        hour = hour - 0.1;

    //the hour angle is zero at noon and has the following value in radians at any time t
    // given in hours and decimal fraction:
    double hourangle = (hour / 12.0 - 1.0) * Math.PI;
    return hourangle;

}

From source file:TimeDate.Date.java

public Date() {
    DateTime datr = new DateTime();
    ano = datr.getYear();//from   ww w  .ja v  a 2s.co  m
    mes = datr.getMonthOfYear();
    dia = datr.getDayOfMonth();
    diasdoano = datr.getDayOfYear();
    semanasdoano = datr.getWeekyear();
    locale = new Langs.Locale();
}