Example usage for org.joda.time MutableDateTime setHourOfDay

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

Introduction

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

Prototype

public void setHourOfDay(final int hourOfDay) 

Source Link

Document

Set the hour of the day to the specified value.

Usage

From source file:org.talend.components.netsuite.avro.converter.XMLGregorianCalendarToDateTimeConverter.java

License:Open Source License

@Override
public Object convertToAvro(XMLGregorianCalendar xts) {
    if (xts == null) {
        return null;
    }//from w w  w.j av  a 2s.c  o  m

    MutableDateTime dateTime = new MutableDateTime();
    try {
        dateTime.setYear(xts.getYear());
        dateTime.setMonthOfYear(xts.getMonth());
        dateTime.setDayOfMonth(xts.getDay());
        dateTime.setHourOfDay(xts.getHour());
        dateTime.setMinuteOfHour(xts.getMinute());
        dateTime.setSecondOfMinute(xts.getSecond());
        dateTime.setMillisOfSecond(xts.getMillisecond());

        DateTimeZone tz = DateTimeZone.forOffsetMillis(xts.getTimezone() * 60000);
        if (tz != null) {
            dateTime.setZoneRetainFields(tz);
        }

        return Long.valueOf(dateTime.getMillis());
    } catch (IllegalArgumentException e) {
        throw new ComponentException(e);
    }
}

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//from   w  ww  .j  a v a  2  s.  c  o m
        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.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  a2s. c o m
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();
}

From source file:y.elf.DbReader.java

License:Open Source License

public static List<ElfValue> readOldCentralineFile(String filename, int defaultvalue, int fieldnn) {
    final String separatorDate = "/";
    final String separatorTime = ":";
    final int UNUSED = Integer.MIN_VALUE;

    List<ElfValue> list = new ArrayList<ElfValue>();

    BufferedReader reader = null;

    try {/*from  www  .  j a va  2s  .  c  o  m*/
        reader = new BufferedReader(new FileReader(filename));
        String line;
        MutableDateTime lasttime = new MutableDateTime(0, 1, 1, 0, 0, 0, 0);
        final MutableDateTime invalidtime = new MutableDateTime(0, 1, 1, 0, 0, 0, 0);

        while ((line = reader.readLine()) != null) {
            if (line.isEmpty())
                continue;

            try {
                boolean valid = true;
                final String[] parts = tokenize(line);
                int value = UNUSED;
                int maxvalue = 0;

                for (int i = 0; i < parts.length; i++) {
                    if (parts[i].equals("*"))
                        valid = false;
                    else if (parts[i].contains(separatorDate)) {
                        final String[] date = parts[i].split(separatorDate);

                        lasttime.setDayOfMonth(Integer.parseInt(date[0]));
                        lasttime.setMonthOfYear(Integer.parseInt(date[1]));
                        lasttime.setYear(Integer.parseInt(date[2]));
                    } else if (parts[i].contains(separatorTime)) {
                        final String[] hour = parts[i].split(separatorTime);
                        lasttime.setHourOfDay(Integer.parseInt(hour[0]));
                        lasttime.setMinuteOfHour(Integer.parseInt(hour[1]));
                    } else if (value == UNUSED)
                        value = translateValue(parts[i], defaultvalue);
                    else
                        maxvalue = translateValue(parts[i], defaultvalue);
                }

                if (lasttime.equals(invalidtime)) // invalid line (header)
                    continue;

                final DateTime currentdatetime = lasttime.toDateTime();

                if (value > 0) {
                    if (fieldnn == 2)
                        list.add(new ElfValue(currentdatetime, maxvalue, value, valid));
                    else
                        list.add(new ElfValue(currentdatetime, value, maxvalue, valid));
                } else
                    list.add(new ElfValue(currentdatetime, 0, 0, false));
            } catch (Exception e) {
                continue;
            } // on error, skip line
        }
    } catch (Exception e) {
        //         System.out.println(e.getMessage());
    } finally {
        if (reader != null)
            try {
                reader.close();
            } catch (IOException e) {
            }
    }

    return list;
}