Example usage for org.joda.time MutableDateTime copy

List of usage examples for org.joda.time MutableDateTime copy

Introduction

In this page you can find the example usage for org.joda.time MutableDateTime copy.

Prototype

public MutableDateTime copy() 

Source Link

Document

Clone this object without having to cast the returned object.

Usage

From source file:com.tmathmeyer.sentinel.ui.views.year.YearCalendar.java

License:Open Source License

/**
 * //from  ww  w .ja v  a2  s  .c o m
 * @param mdt a mutable date time on which to start the calendar. this can
 *            be used to make the dynamic month scrolling work if needed.
 */
private void drawCalendar(MutableDateTime mdt) {
    this.removeAll();
    mdt = this.calendarStart = getStartOfYearCalendar(mdt.toDateTime());

    MutableDateTime upperBound = mdt.copy();
    upperBound.addDays(378);
    getVisibleEvents(mdt.toDateTime(), upperBound.toDateTime());

    MutableDateTime yearDate = mdt.copy();
    yearDate.addDays(20);
    int year = yearDate.getYear();

    JLabel title = new JLabel(year + "");
    title.setHorizontalAlignment(SwingConstants.CENTER);
    title.setFont(new Font("DejaVu Sans", Font.BOLD, 25));
    JPanel content = new JPanel();

    this.add(title, BorderLayout.NORTH);
    this.add(content, BorderLayout.CENTER);

    MutableDateTime monthCounter = mdt.copy();

    content.setLayout(new BoxLayout(content, 0));
    for (int i = 0; i < 3; i++) {
        content.add(new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(65566, 0)));
        content.add(getFourMonthLabel(monthCounter.copy()));
        content.add(getFourMonthGrid(mdt.copy()));
        content.add(new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(65566, 0)));
        mdt.addDays(18 * 7);
        monthCounter.addMonths(4);
    }
}

From source file:org.wso2.analytics.esb.util.TimeRangeUtils.java

License:Open Source License

public static List<TimeRange> getDateTimeRanges(long from, long to) {
    List<TimeRange> ranges = new ArrayList<>(10);
    MutableDateTime fromDate = new MutableDateTime(from);
    fromDate.set(DateTimeFieldType.millisOfSecond(), 0);
    MutableDateTime toDate = new MutableDateTime(to);
    toDate.set(DateTimeFieldType.millisOfSecond(), 0);
    MutableDateTime tempFromTime = fromDate.copy();
    MutableDateTime tempToTime = toDate.copy();

    if (log.isDebugEnabled()) {
        log.debug("Time range: " + formatter.format(fromDate.toDate()) + " -> "
                + formatter.format(toDate.toDate()));
    }//from w ww  .j  a  va  2  s .  c  o  m

    if (toDate.getMillis() - fromDate.getMillis() < DateTimeConstants.MILLIS_PER_MINUTE) {
        ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                new long[] { fromDate.getMillis(), toDate.getMillis() }));
    } else {
        if (tempFromTime.getSecondOfMinute() != 0
                && (toDate.getMillis() - fromDate.getMillis() > DateTimeConstants.MILLIS_PER_MINUTE)) {
            tempFromTime = tempFromTime.minuteOfHour().roundCeiling();
            ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getMinuteOfHour() != 0
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) {
            fromDate = tempFromTime.copy();
            if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE) < 60) {
                tempFromTime = tempFromTime.minuteOfHour().add(
                        (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE);
            } else {
                tempFromTime = tempFromTime.hourOfDay().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.MINUTE.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getHourOfDay() != 0
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) {
            fromDate = tempFromTime.copy();
            if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR) < 24) {
                tempFromTime = tempFromTime.hourOfDay().add(
                        (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR);
            } else {
                tempFromTime = tempFromTime.dayOfMonth().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.HOUR.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getDayOfMonth() != 1
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) {
            fromDate = tempFromTime.copy();
            if ((((toDate.getMillis() - tempFromTime.getMillis())
                    / DateTimeConstants.MILLIS_PER_DAY)) < tempFromTime.dayOfMonth().getMaximumValue()) {
                tempFromTime = tempFromTime.dayOfMonth().add(((toDate.getMillis() - tempFromTime.getMillis())
                        / ((long) DateTimeConstants.MILLIS_PER_DAY)));
            } else {
                tempFromTime = tempFromTime.monthOfYear().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.DAY.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempToTime.getSecondOfMinute() != 0
                && (tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_SECOND) {
            toDate = tempToTime.copy();
            long remainingSeconds = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_MINUTE) / DateTimeConstants.MILLIS_PER_SECOND;
            if (remainingSeconds < 60) {
                tempToTime = tempToTime.secondOfMinute().add(-1 * remainingSeconds);

            } else {
                tempToTime = tempToTime.secondOfMinute().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getMinuteOfHour() != 0 && ((tempToTime.getMillis()
                - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) {
            toDate = tempToTime.copy();
            long remainingMinutes = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_HOUR) / DateTimeConstants.MILLIS_PER_MINUTE;
            if (remainingMinutes < 60) {
                tempToTime = tempToTime.minuteOfHour().add(-1 * remainingMinutes);
            } else {
                tempToTime = tempToTime.hourOfDay().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.MINUTE.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getHourOfDay() != 0
                && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) {
            toDate = tempToTime.copy();
            long remainingHours = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_DAY) / DateTimeConstants.MILLIS_PER_HOUR;
            if (remainingHours < 24) {
                tempToTime = tempToTime.hourOfDay().add(-1 * remainingHours);
            } else {
                tempToTime = tempToTime.dayOfMonth().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.HOUR.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getDayOfMonth() != 1
                && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) {
            toDate = tempToTime.copy();
            tempToTime = tempToTime.monthOfYear().roundFloor();
            ranges.add(new TimeRange(RangeUnit.DAY.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.isAfter(tempFromTime)) {
            ranges.add(new TimeRange(RangeUnit.MONTH.name(),
                    new long[] { tempFromTime.getMillis(), tempToTime.getMillis() }));
        }
    }
    if (log.isDebugEnabled()) {
        for (TimeRange timeRange : ranges) {
            log.debug("Unit: " + timeRange.getUnit() + " Range: "
                    + formatter.format(new Date(timeRange.getRange()[0])) + "(" + timeRange.getRange()[0]
                    + ")->" + formatter.format(new Date(timeRange.getRange()[1])) + "("
                    + timeRange.getRange()[1] + ")");
        }
    }
    return ranges;
}

From source file:org.wso2.analytics.shared.util.time.TimeRangeUtils.java

License:Open Source License

public static List<TimeRange> getDateTimeRanges(long from, long to) {
    List<TimeRange> ranges = new ArrayList<>(10);
    MutableDateTime fromDate = new MutableDateTime(from);
    fromDate.set(DateTimeFieldType.millisOfSecond(), 0);
    MutableDateTime toDate = new MutableDateTime(to);
    toDate.set(DateTimeFieldType.millisOfSecond(), 0);
    MutableDateTime tempFromTime = fromDate.copy();
    MutableDateTime tempToTime = toDate.copy();

    if (log.isDebugEnabled()) {
        log.debug("Time range: " + formatter.format(fromDate.toDate()) + "->"
                + formatter.format(toDate.toDate()));
    }//from   w w w . j av a2  s .  c  o m

    if (toDate.getMillis() - fromDate.getMillis() < DateTimeConstants.MILLIS_PER_MINUTE) {
        ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                new long[] { fromDate.getMillis(), toDate.getMillis() }));
    } else {
        if (tempFromTime.getSecondOfMinute() != 0
                && (toDate.getMillis() - fromDate.getMillis() > DateTimeConstants.MILLIS_PER_MINUTE)) {
            tempFromTime = tempFromTime.minuteOfHour().roundCeiling();
            ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getMinuteOfHour() != 0
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) {
            fromDate = tempFromTime.copy();
            if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE) < 60) {
                tempFromTime = tempFromTime.minuteOfHour().add(
                        (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE);
            } else {
                tempFromTime = tempFromTime.hourOfDay().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.MINUTE.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getHourOfDay() != 0
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) {
            fromDate = tempFromTime.copy();
            if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR) < 24) {
                tempFromTime = tempFromTime.hourOfDay().add(
                        (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR);
            } else {
                tempFromTime = tempFromTime.dayOfMonth().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.HOUR.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getDayOfMonth() != 1
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) {
            fromDate = tempFromTime.copy();
            if ((((toDate.getMillis() - tempFromTime.getMillis())
                    / DateTimeConstants.MILLIS_PER_DAY)) < tempFromTime.dayOfMonth().getMaximumValue()) {
                tempFromTime = tempFromTime.dayOfMonth().add(((toDate.getMillis() - tempFromTime.getMillis())
                        / ((long) DateTimeConstants.MILLIS_PER_DAY)));
            } else {
                tempFromTime = tempFromTime.monthOfYear().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.DAY.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempToTime.getSecondOfMinute() != 0
                && (tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_SECOND) {
            toDate = tempToTime.copy();
            long remainingSeconds = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_MINUTE) / DateTimeConstants.MILLIS_PER_SECOND;
            if (remainingSeconds < 60) {
                tempToTime = tempToTime.secondOfMinute().add(-1 * remainingSeconds);

            } else {
                tempToTime = tempToTime.secondOfMinute().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getMinuteOfHour() != 0 && ((tempToTime.getMillis()
                - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) {
            toDate = tempToTime.copy();
            long remainingMinutes = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_HOUR) / DateTimeConstants.MILLIS_PER_MINUTE;
            if (remainingMinutes < 60) {
                tempToTime = tempToTime.minuteOfHour().add(-1 * remainingMinutes);
            } else {
                tempToTime = tempToTime.hourOfDay().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.MINUTE.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getHourOfDay() != 0
                && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) {
            toDate = tempToTime.copy();
            long remainingHours = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_DAY) / DateTimeConstants.MILLIS_PER_HOUR;
            if (remainingHours < 24) {
                tempToTime = tempToTime.hourOfDay().add(-1 * remainingHours);
            } else {
                tempToTime = tempToTime.dayOfMonth().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.HOUR.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getDayOfMonth() != 1
                && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) {
            toDate = tempToTime.copy();
            tempToTime = tempToTime.monthOfYear().roundFloor();
            ranges.add(new TimeRange(RangeUnit.DAY.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.isAfter(tempFromTime)) {
            ranges.add(new TimeRange(RangeUnit.MONTH.name(),
                    new long[] { tempFromTime.getMillis(), tempToTime.getMillis() }));
        }
    }
    if (log.isDebugEnabled()) {
        for (TimeRange timeRange : ranges) {
            log.debug("Unit: " + timeRange.getUnit() + " Range: "
                    + formatter.format(new Date(timeRange.getRange()[0])) + "(" + timeRange.getRange()[0]
                    + ")->" + formatter.format(new Date(timeRange.getRange()[1])) + "("
                    + timeRange.getRange()[1] + ")");
        }
    }
    return ranges;
}

From source file:se.streamsource.streamflow.web.context.crystal.CrystalContext.java

License:Apache License

public TableValue motionchart() {
    final TableBuilder tableBuilder = new TableBuilder(module.valueBuilderFactory());
    tableBuilder.column("CaseType", "Case type", "string").column("Week", "Week", "string")
            .column("Variation", "Variation", "number").column("Duration", "Duration", "number")
            .column("CaseCount", "Case count", "number").column("CasetypeOwner", "Casetype owner", "string");

    final Logger logger = LoggerFactory.getLogger(getClass());
    try {/*from  w w w .  ja  v a  2 s .  com*/
        final String weekFormat = "yyyy'W'ww";

        DateTime[] range = findRange();
        logger.info(
                "Full range from " + range[0].toString(weekFormat) + " to " + range[1].toString(weekFormat));

        // Find cases for each week
        Databases databases = new Databases(source.get());

        final MutableDateTime from = new MutableDateTime(range[0]).dayOfWeek().set(1);

        while (from.isBefore(range[1])) {

            final MutableDateTime minWeek = from.copy();

            databases.query(sql.getString("motionchart"), new Databases.StatementVisitor() {
                public void visit(PreparedStatement preparedStatement) throws SQLException {
                    String fromWeek = from.toString(weekFormat);

                    preparedStatement.setTimestamp(1, new Timestamp(from.toDate().getTime()));
                    from.addWeeks(1);
                    preparedStatement.setTimestamp(2, new Timestamp(from.toDate().getTime()));
                    String toWeek = from.toString(weekFormat);

                    logger.info("From " + fromWeek + " to " + toWeek);
                }
            }, new Databases.ResultSetVisitor() {
                public boolean visit(ResultSet visited) throws SQLException {
                    tableBuilder.row().cell(visited.getString("casetype"), null)
                            .cell(minWeek.toString(weekFormat), "v" + minWeek.weekOfWeekyear().get())
                            .cell(visited.getString("variationpct"), null)
                            .cell((visited.getLong("average") / (1000 * 60 * 60)) + "", null)
                            .cell(visited.getString("count"), null)
                            .cell(visited.getString("casetype_owner"), null);

                    return true;
                }
            });
        }

    } catch (SQLException e) {
        logger.warn("Could not get statistics", e);
    }

    return tableBuilder.newTable();
}