Example usage for org.joda.time MutableDateTime MutableDateTime

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

Introduction

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

Prototype

public MutableDateTime(Object instant) 

Source Link

Document

Constructs an instance from an Object that represents a datetime.

Usage

From source file:org.wicketstuff.calendar.markup.html.form.DateField.java

License:Apache License

/**
 * Sets date.//ww w  . j  ava2 s.  co  m
 * 
 * @param date
 *            date
 */
public void setDate(Date date) {
    this.date = (date != null) ? new MutableDateTime(date) : null;
    setModelObject(date);
}

From source file:org.wicketstuff.calendar.markup.html.form.DateField.java

License:Apache License

/**
 * @see org.apache.wicket.Component#onAttach()
 *//*from www.j av  a2  s .  com*/
protected void onBeforeRender() {

    Date d = (Date) getModelObject();
    if (d != null) {
        date = new MutableDateTime(d);
    } else {
        date = null;
    }

    super.onBeforeRender();
}

From source file:org.wicketstuff.calendar.markup.html.form.DateTimeField.java

License:Apache License

/**
 * Sets date.//  ww  w  .ja  v a 2 s  .  c om
 * 
 * @param date
 *            date
 */
public void setDate(Date date) {
    this.date = (date != null) ? new MutableDateTime(date) : null;
}

From source file:org.wicketstuff.datetime.extensions.yui.calendar.DateTimeField.java

License:Apache License

/**
 * @see org.apache.wicket.Component#onBeforeRender()
 *///from   w w w .  ja  v  a  2s  .  co m
@Override
protected void onBeforeRender() {
    dateField.setRequired(isRequired());
    hoursField.setRequired(isRequired());
    minutesField.setRequired(isRequired());

    boolean use12HourFormat = use12HourFormat();
    amOrPmChoice.setVisible(use12HourFormat);

    Date modelObject = (Date) getDefaultModelObject();
    if (modelObject == null) {
        date = null;
        hours = null;
        minutes = null;
    } else {
        MutableDateTime mDate = new MutableDateTime(modelObject);
        // convert date to the client's time zone if we have that info
        TimeZone zone = getClientTimeZone();
        if (zone != null) {
            mDate.setZone(DateTimeZone.forTimeZone(zone));
        }

        date = mDate.toDateTime().toLocalDate().toDate();

        if (use12HourFormat) {
            int hourOfHalfDay = mDate.get(DateTimeFieldType.hourOfHalfday());
            hours = hourOfHalfDay == 0 ? 12 : hourOfHalfDay;
        } else {
            hours = mDate.get(DateTimeFieldType.hourOfDay());
        }

        amOrPm = (mDate.get(DateTimeFieldType.halfdayOfDay()) == 0) ? AM_PM.AM : AM_PM.PM;
        minutes = mDate.getMinuteOfHour();
    }

    super.onBeforeRender();
}

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()));
    }/*ww  w.  java  2s  .  co 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()));
    }// w ww . j a  v  a 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 long getFloorValueForTimeStamp(long timestamp, String timeUnit) {
    RangeUnit timeUnitEnum = RangeUnit.valueOf(timeUnit);
    MutableDateTime mutableDateTime = new MutableDateTime(timestamp);
    long newFloorTimestamp = mutableDateTime.getMillis();
    switch (timeUnitEnum) {
    case MONTH:/*ww  w.ja  v  a 2s. com*/
        newFloorTimestamp = mutableDateTime.monthOfYear().roundFloor().getMillis();
        break;
    case DAY:
        newFloorTimestamp = mutableDateTime.dayOfMonth().roundFloor().getMillis();
        break;
    case HOUR:
        newFloorTimestamp = mutableDateTime.hourOfDay().roundFloor().getMillis();
        break;
    }
    if (log.isDebugEnabled()) {
        log.debug("Original date: " + formatter.format(new Date(timestamp)) + " -> Rounded date:"
                + formatter.format(new Date(newFloorTimestamp)));
    }
    return newFloorTimestamp;
}

From source file:rapture.event.generator.RangedEventGenerator.java

License:Open Source License

public static List<TimedEventRecord> generateWeeklyEvents(DateTime start) {
    List<TimedEventRecord> ret = new ArrayList<TimedEventRecord>();
    ret.addAll(EventGenerator.generateWeeklyEventRecords(start, new EventHelper() {

        @Override/*  w  w  w.  j a  v a 2s .  com*/
        public List<TimedEventRecord> filterEvent(String eventName, DateTime forWhen) {
            String scriptPrefix = "/system/timeprocessor" + eventName;
            List<TimedEventRecord> ret = new ArrayList<TimedEventRecord>();
            scriptWorkWith(eventName, scriptPrefix, forWhen, ret, 1);
            eventWorkWith(eventName, "/" + eventName, forWhen, ret, 1);
            return ret;
        }

        private ReflexRaptureScript rrs = new ReflexRaptureScript();

        private void eventWorkWith(String eventName, String prefix, DateTime forWhen,
                List<TimedEventRecord> ret, int depth) {
            List<RaptureFolderInfo> fInfo = Kernel.getEvent()
                    .listEventsByUriPrefix(ContextFactory.getKernelUser(), prefix);
            for (RaptureFolderInfo f : fInfo) {
                String next = prefix + "/" + f.getName();
                if (f.isFolder()) {
                    depth++;
                    if (depth != 4) {
                        eventWorkWith(eventName, next, forWhen, ret, depth);
                    }
                } else {
                    RaptureEvent event = Kernel.getEvent().getEvent(ContextFactory.getKernelUser(), next);
                    // Now for each of the event items, add a timedRecord
                    if (event.getScripts() != null) {
                        for (RaptureEventScript script : event.getScripts()) {
                            ret.add(getEventRecordForScript(eventName, script.getScriptURI(), forWhen));
                        }
                    }
                    if (event.getWorkflows() != null) {
                        for (RaptureEventWorkflow workflow : event.getWorkflows()) {
                            ret.add(getEventRecordForWorkflow(next, workflow.getWorkflow(), forWhen));
                        }
                    }
                }
            }
        }

        private TimedEventRecord getEventRecordForWorkflow(String eventName, String workflowId,
                DateTime forWhen) {
            // The record will have a modified timezone string first, then the hour, then the minute, we need
            // to convert that hour/minute/timezone into a local date (or actually a datetime in a given timezone usually passed in)

            String[] parts = eventName.split("/");
            String timeZoneString = parts[parts.length - 3];
            String hourString = parts[parts.length - 2];
            String minuteString = parts[parts.length - 1];
            DateTimeZone tz = EventGenerator.getRealDateTimeZone(timeZoneString);

            MutableDateTime dt = new MutableDateTime(tz);
            dt.setHourOfDay(Integer.parseInt(hourString));
            dt.setMinuteOfHour(Integer.parseInt(minuteString));

            DateTime inLocalTimeZone = dt.toDateTime(forWhen.getZone());

            TimedEventRecord rec = new TimedEventRecord();
            // Workflow w =
            // Kernel.getDecision().getWorkflow(ContextFactory.getKernelUser(),
            // workflowId);
            rec.setEventName(workflowId);
            rec.setEventContext(workflowId);
            rec.setWhen(inLocalTimeZone.toDate());
            rec.setInfoContext("green");

            DateTime endPoint = inLocalTimeZone.plusMinutes(30);
            rec.setEnd(endPoint.toDate());
            return rec;
        }

        private TimedEventRecord getEventRecordForScript(String eventName, String scriptId, DateTime forWhen) {
            String[] parts = eventName.split("/");
            String timeZoneString = parts[parts.length - 3];
            String hourString = parts[parts.length - 2];
            String minuteString = parts[parts.length - 1];
            DateTimeZone tz = EventGenerator.getRealDateTimeZone(timeZoneString);

            MutableDateTime dt = new MutableDateTime(tz);
            dt.setDate(forWhen.getMillis());
            dt.setHourOfDay(Integer.parseInt(hourString));
            dt.setMinuteOfHour(Integer.parseInt(minuteString));

            DateTime inLocalTimeZone = dt.toDateTime(forWhen.getZone());

            TimedEventRecord rec = new TimedEventRecord();
            // Put the name from the meta property "name" on the
            // script if it exists
            RaptureScript theScript = Kernel.getScript().getScript(ContextFactory.getKernelUser(), scriptId);
            String name = null;
            String color = "blue";
            if (theScript != null) {
                try {
                    ReflexParser parser = rrs.getParser(ContextFactory.getKernelUser(), theScript.getScript());
                    name = parser.scriptInfo.getProperty("name");
                    if (parser.scriptInfo.getProperty("color") != null) {
                        color = parser.scriptInfo.getProperty("color");
                    }
                } catch (Exception e) {
                    log.error("Unexpected error: " + ExceptionToString.format(e));
                }
            }
            rec.setEventName(name == null ? eventName : name);
            rec.setEventContext(scriptId);
            rec.setInfoContext(color);
            rec.setWhen(inLocalTimeZone.toDate());
            DateTime endPoint = inLocalTimeZone.plusMinutes(30);
            rec.setEnd(endPoint.toDate());
            return rec;
        }

        private void scriptWorkWith(String eventName, String prefix, DateTime forWhen,
                List<TimedEventRecord> ret, int depth) {
            Map<String, RaptureFolderInfo> fInfo = Kernel.getScript()
                    .listScriptsByUriPrefix(ContextFactory.getKernelUser(), prefix, -1);
            for (RaptureFolderInfo f : fInfo.values()) {
                String next = prefix + "/" + f.getName();
                if (f.isFolder()) {
                    if (depth != 4) {
                        scriptWorkWith(eventName, next, forWhen, ret, depth + 1);
                    } else {
                    }
                } else {
                    // This script is set to run at x/y/z.../02/00
                    // So run
                    ret.add(getEventRecordForScript(next, next, forWhen));
                }
            }
        }

    }));

    return ret;
}

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 . j  av a2s. c om*/
        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();
}

From source file:se.toxbee.sleepfighter.model.Alarm.java

License:Open Source License

/**
 * Returns when this alarm will ring.<br/>
 * If {@link #canHappen()} returns false, -1 will be returned.
 *
 * @param now the current time in unix epoch timestamp.
 * @return the time in unix epoch timestamp when alarm will next ring.
 *///w  w  w.  j  a  v a  2  s . com
public synchronized Long getNextMillis(long now) {
    if (!this.canHappen()) {
        return NEXT_NON_REAL;
    }

    MutableDateTime next = new MutableDateTime(now);
    next.setHourOfDay(this.hour);
    next.setMinuteOfHour(this.minute);
    next.setSecondOfMinute(this.second);

    // Check if alarm was earlier today. If so, move to next day
    if (next.isBefore(now)) {
        next.addDays(1);
    }
    // Offset for weekdays
    int offset = 0;

    // first weekday to check (0-6), getDayOfWeek returns (1-7)
    int weekday = next.getDayOfWeek() - 1;

    // Find the weekday the alarm should run, should at most run seven times
    for (int i = 0; i < 7; i++) {
        // Wrap to first weekday
        if (weekday > MAX_WEEK_INDEX) {
            weekday = 0;
        }
        if (this.enabledDays[weekday]) {
            // We've found the closest day the alarm is enabled for
            offset = i;
            break;
        }
        weekday++;
        offset++;
    }

    if (offset > 0) {
        next.addDays(offset);
    }

    return next.getMillis();
}