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:com.josecalles.tistiq.task.StatCalculator.java

License:Apache License

public int mostMessagesSentInADay(RealmList<TextMessage> messages) {
    ArrayList<String> daysOfYear = new ArrayList<>();
    for (TextMessage message : messages) {
        DateTime time = new DateTime(message.getDateReceived());
        String dayOfYear = Integer.toString(time.getDayOfYear());
        daysOfYear.add(dayOfYear);// w  ww  .  ja va 2s .c o m
    }

    HashMap<String, Integer> daysWithCount = new HashMap<>();
    for (int i = 0; i < daysOfYear.size(); i++) {
        if (daysWithCount.containsKey(daysOfYear.get(i))) {
            daysWithCount.put(daysOfYear.get(i), (daysWithCount.get(daysOfYear.get(i)) + 1));
        } else {
            daysWithCount.put(daysOfYear.get(i), 1);
        }
    }
    String numberOfMessages = sortValues(daysWithCount, 1, true).get(0);
    return Integer.parseInt(numberOfMessages);
}

From source file:com.josecalles.tistiq.task.StatCalculator.java

License:Apache License

public int mostMessagesReceivedInADay(RealmList<TextMessage> messages) {
    ArrayList<String> daysOfYear = new ArrayList<>();
    for (TextMessage message : messages) {
        DateTime time = new DateTime(message.getDateReceived());
        String dayOfYear = Integer.toString(time.getDayOfYear());
        daysOfYear.add(dayOfYear);//  w  w w .  j av  a  2  s . com
    }

    HashMap<String, Integer> daysWithCount = new HashMap<>();
    for (int i = 0; i < daysOfYear.size(); i++) {
        if (daysWithCount.containsKey(daysOfYear.get(i))) {
            daysWithCount.put(daysOfYear.get(i), (daysWithCount.get(daysOfYear.get(i)) + 1));
        } else {
            daysWithCount.put(daysOfYear.get(i), 1);
        }
    }
    String numberOfMessages = sortValues(daysWithCount, 1, true).get(0);
    return Integer.parseInt(numberOfMessages);
}

From source file:com.mastfrog.acteur.headers.DateTimeHeader.java

License:Open Source License

@Override
public DateTime toValue(String value) {
    long val = 0;
    if (val == 0) {
        try {//from ww  w  .j  av  a 2 s  . com
            val = Headers.ISO2822DateFormat.parseDateTime(value).getMillis();
        } catch (IllegalArgumentException e) {
            try {
                //Sigh...use java.util.date to handle "GMT", "PST", "EST"
                val = Date.parse(value);
            } catch (IllegalArgumentException ex) {
                new IllegalArgumentException(value, ex).printStackTrace();
                return null;
            }
        }
    }
    DateTime result = new DateTime(val, DateTimeZone.UTC);
    //to be truly compliant, accept 2-digit dates
    if (result.getYear() < 100 && result.getYear() > 0) {
        if (result.getYear() >= 50) {
            result = result.withYear(2000 - (100 - result.getYear())).withDayOfYear(result.getDayOfYear() - 1); //don't ask
        } else {
            result = result.withYear(2000 + result.getYear());
        }
    }
    return result;
}

From source file:com.netflix.ice.processor.BillingFileProcessor.java

License:Apache License

private void archiveSummary(Map<Product, ReadWriteData> dataMap, String prefix) throws Exception {

    DateTime monthDateTime = new DateTime(startMilli, DateTimeZone.UTC);

    for (Product product : dataMap.keySet()) {

        String prodName = product == null ? "all" : product.name;
        ReadWriteData data = dataMap.get(product);
        Collection<TagGroup> tagGroups = data.getTagGroups();

        // init daily, weekly and monthly
        List<Map<TagGroup, Double>> daily = Lists.newArrayList();
        List<Map<TagGroup, Double>> weekly = Lists.newArrayList();
        List<Map<TagGroup, Double>> monthly = Lists.newArrayList();

        // get last month data
        ReadWriteData lastMonthData = new DataWriter(prefix + "hourly_" + prodName + "_"
                + AwsUtils.monthDateFormat.print(monthDateTime.minusMonths(1)), true).getData();

        // aggregate to daily, weekly and monthly
        int dayOfWeek = monthDateTime.getDayOfWeek();
        int daysFromLastMonth = dayOfWeek - 1;
        int lastMonthNumHours = monthDateTime.minusMonths(1).dayOfMonth().getMaximumValue() * 24;
        for (int hour = 0 - daysFromLastMonth * 24; hour < data.getNum(); hour++) {
            if (hour < 0) {
                // handle data from last month, add to weekly
                Map<TagGroup, Double> prevData = lastMonthData.getData(lastMonthNumHours + hour);
                for (TagGroup tagGroup : tagGroups) {
                    Double v = prevData.get(tagGroup);
                    if (v != null && v != 0) {
                        addValue(weekly, 0, tagGroup, v);
                    }//from w  w  w. j  a  va2s  .co m
                }
            } else {
                // this month, add to weekly, monthly and daily
                Map<TagGroup, Double> map = data.getData(hour);

                for (TagGroup tagGroup : tagGroups) {
                    Double v = map.get(tagGroup);
                    if (v != null && v != 0) {
                        addValue(monthly, 0, tagGroup, v);
                        addValue(daily, hour / 24, tagGroup, v);
                        addValue(weekly, (hour + daysFromLastMonth * 24) / 24 / 7, tagGroup, v);
                    }
                }
            }
        }

        // archive daily
        int year = monthDateTime.getYear();
        DataWriter writer = new DataWriter(prefix + "daily_" + prodName + "_" + year, true);
        ReadWriteData dailyData = writer.getData();
        dailyData.setData(daily, monthDateTime.getDayOfYear() - 1, false);
        writer.archive();

        // archive monthly
        writer = new DataWriter(prefix + "monthly_" + prodName, true);
        ReadWriteData monthlyData = writer.getData();
        monthlyData.setData(monthly, Months.monthsBetween(config.startDate, monthDateTime).getMonths(), false);
        writer.archive();

        // archive weekly
        writer = new DataWriter(prefix + "weekly_" + prodName, true);
        ReadWriteData weeklyData = writer.getData();
        DateTime weekStart = monthDateTime.withDayOfWeek(1);
        int index;
        if (!weekStart.isAfter(config.startDate))
            index = 0;
        else
            index = Weeks.weeksBetween(config.startDate, weekStart).getWeeks()
                    + (config.startDate.dayOfWeek() == weekStart.dayOfWeek() ? 0 : 1);
        weeklyData.setData(weekly, index, true);
        writer.archive();
    }
}

From source file:com.serotonin.mango.util.DateUtils.java

License:Open Source License

public static DateTime truncateDateTime(DateTime time, int periodType) {
    if (periodType == TimePeriods.SECONDS)
        time = time.minus(time.getMillisOfSecond());
    else if (periodType == TimePeriods.MINUTES) {
        time = time.minus(time.getMillisOfSecond());
        time = time.minus(Common.getPeriod(TimePeriods.SECONDS, time.getSecondOfMinute()));
    } else if (periodType == TimePeriods.HOURS) {
        time = time.minus(time.getMillisOfSecond());
        time = time.minus(Common.getPeriod(TimePeriods.SECONDS, time.getSecondOfMinute()));
        time = time.minus(Common.getPeriod(TimePeriods.MINUTES, time.getMinuteOfHour()));
    } else if (periodType == TimePeriods.DAYS) {
        time = time.minus(time.getMillisOfDay());
    } else if (periodType == TimePeriods.WEEKS) {
        time = time.minus(time.getMillisOfDay());
        time = time.minus(Common.getPeriod(TimePeriods.DAYS, time.getDayOfWeek() - 1));
    } else if (periodType == TimePeriods.MONTHS) {
        time = time.minus(time.getMillisOfDay());
        time = time.minus(Common.getPeriod(TimePeriods.DAYS, time.getDayOfMonth() - 1));
    } else if (periodType == TimePeriods.YEARS) {
        time = time.minus(time.getMillisOfDay());
        time = time.minus(Common.getPeriod(TimePeriods.DAYS, time.getDayOfYear() - 1));
    }//from   www  .j a  va2s .  c  o m
    return time;
}

From source file:com.spotify.heroic.shell.Tasks.java

License:Apache License

private static long parseTodayInstant(String input, final Chronology chrono, long now) {
    final DateTime n = new DateTime(now, chrono);

    for (final DateTimeParser p : today) {
        final DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, null, null, 2000);

        bucket.saveField(chrono.year(), n.getYear());
        bucket.saveField(chrono.monthOfYear(), n.getMonthOfYear());
        bucket.saveField(chrono.dayOfYear(), n.getDayOfYear());

        try {//from w  w w .  ja  v  a2 s.c  om
            p.parseInto(bucket, input, 0);
        } catch (IllegalArgumentException e) {
            // pass-through
            continue;
        }

        return bucket.computeMillis();
    }

    throw new IllegalArgumentException(input + " is not a valid instant");
}

From source file:com.sqewd.os.maracache.api.utils.TimeUtils.java

License:Apache License

/**
 * Get the time bucket for the input date based on the Time Unit and Unit multiplier specified.
 *
 * @param dt         - Date time to bucket.
 * @param unit       - Time Unit//from  w w w . j a  v a 2 s  .  c  om
 * @param multiplier - Unit multiplier.
 * @return
 */
public static DateTime bucket(DateTime dt, TimeUnit unit, int multiplier) {
    DateTime w = null;
    switch (unit) {
    case MILLISECONDS:
        int ms = (dt.getMillisOfSecond() / multiplier) * multiplier;
        w = dt.secondOfMinute().roundFloorCopy().plusMillis(ms);
        break;
    case SECONDS:
        int s = (dt.getSecondOfMinute() / multiplier) * multiplier;
        w = dt.minuteOfHour().roundFloorCopy().plusSeconds(s);
        break;
    case MINUTES:
        int m = (dt.getMinuteOfHour() / multiplier) * multiplier;
        w = dt.hourOfDay().roundFloorCopy().plusMinutes(m);
        break;
    case HOURS:
        int h = (dt.getHourOfDay() / multiplier) * multiplier;
        w = dt.dayOfYear().roundFloorCopy().plusHours(h);
        break;
    case DAYS:
        int d = (dt.getDayOfYear() / multiplier) * multiplier;
        // Need to subtract (1) as the start offset i
        if (dt.getDayOfYear() % multiplier == 0) {
            d -= 1;
        }
        w = dt.yearOfCentury().roundFloorCopy().plusDays(d);
        break;
    }
    return w;
}

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();
        ///* www  .  j av a  2s. 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 Period computeOffset(IntervalleObject presentInterval, IntervalleObject pastInterval,
        AxisValues joinAxis) throws ScopeException {
    // it is better to compare on the lower bound because alignment on the
    // end of month is not accurate
    Object present = presentInterval.getLowerBound();
    Object past = pastInterval.getLowerBound();
    ///*from  w w w  .j a v a  2s  .co  m*/

    IDomain image = joinAxis.getAxis().getDefinition().getImageDomain();
    PeriodType type = computePeriodType(image);
    //
    if (present instanceof Date && past instanceof Date) {

        Period presentPeriod = new Period(new LocalDate(((Date) presentInterval.getLowerBound()).getTime()),
                new LocalDate(((Date) presentInterval.getUpperBound()).getTime()).plusDays(1), type);

        Period pastPeriod = new Period(new LocalDate(((Date) pastInterval.getLowerBound()).getTime()),
                new LocalDate(((Date) pastInterval.getUpperBound()).getTime()).plusDays(1), type);

        Date pastDate = (Date) past;
        DateTime dt = new DateTime(pastDate);
        if (image.isInstanceOf(IDomain.YEARLY)) {
            if (presentPeriod.getYears() > pastPeriod.getYears()) {
                // e.g. presentPeriod of 365 days ->
                // presentPeriod.getYears=1 && past year of 366 days ->
                // pastPeriod.getYears=0
                DateTime newDT = new DateTime(dt.getYear(), 1, 1, 0, 0);
                pastDate = newDT.toDate();
            } else {
                if (dt.getDayOfYear() != 1) {
                    // e.g present period of 366 days -> past date at dec 31
                    DateTime newDT = new DateTime(dt.getYear() + 1, 1, 1, 0, 0);
                    pastDate = newDT.toDate();
                }
            }
        } else if (image.isInstanceOf(IDomain.QUATERLY) || image.isInstanceOf(IDomain.MONTHLY)) {

            if (presentPeriod.getMonths() > pastPeriod.getMonths()) {
                // e.g present period of 28 days(February) ->
                // pastPeriod.getMonths() = 0 (January has 31 days)
                DateTime newDT = new DateTime(dt.getYear(), dt.getMonthOfYear(), 1, 0, 0);
                pastDate = newDT.toDate();
            } else {
                if (dt.getDayOfMonth() != 1) {
                    // e.g. present period of 31 day(March) pastDate = Feb 6
                    if (dt.getMonthOfYear() == 12) {
                        DateTime newDT = new DateTime(dt.getYear() + 1, 1, 1, 0, 0);
                        pastDate = newDT.toDate();
                    } else {
                        DateTime newDT = new DateTime(dt.getYear(), dt.getMonthOfYear() + 1, 1, 0, 0);
                        pastDate = newDT.toDate();
                    }
                }
            }
        } else {
            // daily, keep Date as it is
        }
        return new Period(new LocalDate((pastDate).getTime()), new LocalDate(((Date) present).getTime()), type);

    } else {
        return null;
    }
}

From source file:com.tmathmeyer.sentinel.models.data.Commitment.java

License:Open Source License

@Override
public void setTime(DateTime newTime) {
    MutableDateTime mdt = new MutableDateTime(this.duedate);
    mdt.setDayOfYear(newTime.getDayOfYear());
    mdt.setYear(newTime.getYear());/*from w  w w .  jav  a 2 s . c  o m*/
    this.duedate = mdt.toDate();
}