Example usage for org.joda.time Weeks ONE

List of usage examples for org.joda.time Weeks ONE

Introduction

In this page you can find the example usage for org.joda.time Weeks ONE.

Prototype

Weeks ONE

To view the source code for org.joda.time Weeks ONE.

Click Source Link

Document

Constant representing one week.

Usage

From source file:controllers.api.DashboardsApiController.java

License:Open Source License

private Duration estimateIntervalStep(String interval) {
    Duration step;//from   w  w w  . j av  a 2  s  . c  o m
    switch (interval) {
    case "minute":
        step = Minutes.ONE.toStandardDuration();
        break;
    case "hour":
        step = Hours.ONE.toStandardDuration();
        break;
    case "day":
        step = Days.ONE.toStandardDuration();
        break;
    case "week":
        step = Weeks.ONE.toStandardDuration();
        break;
    case "month":
        step = Days.days(31).toStandardDuration();
        break;
    case "quarter":
        step = Days.days(31 * 3).toStandardDuration();
        break;
    case "year":
        step = Days.days(365).toStandardDuration();
        break;
    default:
        throw new IllegalArgumentException("Invalid duration specified: " + interval);
    }
    return step;
}

From source file:net.schweerelos.timeline.model.Timeline.java

License:Open Source License

private void recalculate() {
    if (start == null || end == null) {
        logger.warn("recalculating aborted, start and/or end is null");
        numSlices = 0;//from   www .  j ava 2s . com
        return;
    }
    Interval interval = new Interval(start, end);

    if (Years.yearsIn(interval).isGreaterThan(Years.ZERO)) {
        // make it start at the start of the current increment mode
        start = start.withDayOfYear(start.dayOfYear().getMinimumValue());
        end = end.withDayOfYear(end.dayOfYear().getMaximumValue());
        interval = new Interval(start, end);

        // figure out number of slices
        numSlices = Years.yearsIn(interval).getYears();
        if (start.plusYears(numSlices).isBefore(end)) {
            numSlices += 1;
        }

        // update label extractor
        sliceLabelExtractor = new SliceLabelExtractor() {
            @Override
            public String extractLabel(DateTime from) {
                return from.year().getAsShortText();
            }
        };

        // update increment
        increment = Years.ONE.toPeriod();
        incrementMode = Mode.Years;
    } else if (Months.monthsIn(interval).isGreaterThan(Months.ZERO)) {
        // make it start at the start of the current increment mode
        start = start.withDayOfMonth(start.dayOfMonth().getMinimumValue());
        end = end.withDayOfMonth(end.dayOfMonth().getMaximumValue());
        interval = new Interval(start, end);

        numSlices = Months.monthsIn(interval).getMonths();
        if (start.plusMonths(numSlices).isBefore(end)) {
            numSlices += 1;
        }

        sliceLabelExtractor = new SliceLabelExtractor() {
            @Override
            public String extractLabel(DateTime from) {
                return from.monthOfYear().getAsShortText();
            }
        };

        increment = Months.ONE.toPeriod();
        incrementMode = Mode.Months;
    } else if (Weeks.weeksIn(interval).isGreaterThan(Weeks.ZERO)) {
        start = start.withDayOfWeek(start.dayOfWeek().getMinimumValue());
        end = end.withDayOfWeek(end.dayOfWeek().getMaximumValue());
        interval = new Interval(start, end);

        numSlices = Weeks.weeksIn(interval).getWeeks();
        if (start.plusWeeks(numSlices).isBefore(end)) {
            numSlices += 1;
        }

        sliceLabelExtractor = new SliceLabelExtractor() {
            @Override
            public String extractLabel(DateTime from) {
                return "W" + from.weekOfWeekyear().getAsShortText();
            }
        };

        increment = Weeks.ONE.toPeriod();
        incrementMode = Mode.Weeks;
    } else {
        numSlices = Days.daysIn(interval).getDays();
        if (start.plusDays(numSlices).isBefore(end)) {
            numSlices += 1;
        }
        if (numSlices == 0) {
            // force at least one day to be drawn
            numSlices = 1;
        }

        sliceLabelExtractor = new SliceLabelExtractor() {
            @Override
            public String extractLabel(DateTime from) {
                return from.dayOfMonth().getAsShortText();
            }
        };

        increment = Days.ONE.toPeriod();
        incrementMode = Mode.Days;
    }

    // reset time of day too
    start = start.withMillisOfDay(start.millisOfDay().getMinimumValue());
    end = end.withMillisOfDay(end.millisOfDay().getMaximumValue());

    // recalculate which intervals are within range
    intervalsWithinRange.clear();
    intervalsWithinRange.addAll(calculateIntervalsWithinRange(start, end));

    // notify listeners
    changeSupport.firePropertyChange(INTERVAL_PROPERTY_KEY, interval, new Interval(start, end));
}