Example usage for org.joda.time DateTime minusHours

List of usage examples for org.joda.time DateTime minusHours

Introduction

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

Prototype

public DateTime minusHours(int hours) 

Source Link

Document

Returns a copy of this datetime minus the specified number of hours.

Usage

From source file:org.richfaces.tests.page.fragments.impl.calendar.common.editor.time.RichFacesTimeEditor.java

License:Open Source License

@Override
public DateTime getTime() {
    int seconds = (getSecondsSpinner() != null ? getSecondsSpinner().getValue() : defaultSeconds);
    int minutes = (getMinutesSpinner() != null ? getMinutesSpinner().getValue() : defaultMinutes);
    int hours = (getHoursSpinner() != null ? getHoursSpinner().getValue() : defaultHours);
    DateTime result = new DateTime().withHourOfDay(hours).withMinuteOfHour(minutes).withSecondOfMinute(seconds);
    RichFacesTimeSignSpinner tss = getTimeSignSpinner();
    if (tss != null) {
        switch (tss.getValue()) {
        case AM:/*from w ww.  j av  a 2 s .c o m*/
            if (result.getHourOfDay() == 12) {//12:xx am -> 00:xx
                result = result.minusHours(12);
            }
            break;
        case PM:
            if (result.getHourOfDay() != 12) {
                result = result.plusHours(12);
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown switch");
        }
    }
    return result;
}

From source file:org.springframework.analytics.rest.controller.AggregateCounterController.java

License:Apache License

/**
 * Return a default value for the interval start if none has been provided.
 *///from ww w  . j a v a  2s.c  o  m
private DateTime providedOrDefaultFromValue(DateTime from, DateTime to, AggregateCounterResolution resolution) {
    if (from != null) {
        return from;
    }
    switch (resolution) {
    case minute:
        return to.minusMinutes(59);
    case hour:
        return to.minusHours(23);
    case day:
        return to.minusDays(6);
    case month:
        return to.minusMonths(11);
    case year:
        return to.minusYears(4);
    default:
        throw new IllegalStateException("Shouldn't happen. Unhandled resolution: " + resolution);
    }
}

From source file:org.springframework.xd.dirt.rest.metrics.AggregateCountersController.java

License:Apache License

/**
 * Return a default value for the interval start if none has been provided.
 *//*from   w  w w .  j a  v  a  2s  .co m*/
private DateTime providedOrDefaultFromValue(DateTime from, DateTime to, AggregateCountResolution resolution) {
    if (from != null) {
        return from;
    }
    switch (resolution) {
    case minute:
        return to.minusMinutes(59);
    case hour:
        return to.minusHours(23);
    case day:
        return to.minusDays(6);
    case month:
        return to.minusMonths(11);
    case year:
        return to.minusYears(4);
    default:
        throw new IllegalStateException("Shouldn't happen. Unhandled resolution: " + resolution);
    }
}

From source file:org.vaadin.spring.samples.mvp.util.SSTimeUtil.java

License:Apache License

/**
 * Determines the hour label corresponding to a org.joda.time.DateTime
 *
 * @param dateTime//from w w w .  j a  va  2  s  .  c om
 *            org.joda.time.DateTime (typically a whole hour)
 * @return an hour label where 01 represents 1:00AM and 24 represents
 *         12:00AM of the following day
 */
public static String dateTimeToHourLabel(final DateTime dateTime) {
    String hour = null;
    if (dateTime != null) {
        hour = hourFormat.print(dateTime.withZone(marketTimeZone));
        final String tz = timeZoneOffsetFormat.print(dateTime);
        if (hour != null) {
            // midnight is the 24th hour!
            if (hour.equals("00")) {
                hour = "24";
            }
            if (hour.equals("01")) {
                final DateTime priorHourAsDate = dateTime.minusHours(1);
                final String priorTz = timeZoneOffsetFormat.print(priorHourAsDate);
                if (!tz.equals(priorTz)) {
                    hour = "02";
                }
            }
            if (isExtraHourForHourEnding(dateTime)) {
                hour = "02*";
            }
        }
    }
    return hour;
}

From source file:org.vaadin.spring.samples.mvp.util.SSTimeUtil.java

License:Apache License

/**
 * Determines whether current hour is an "extra hour" when transitioning
 * from Daylight Savings to Standard Time for a DateTime that represents
 * hour ending data.// w w w  .j a  va 2  s  .  c o m
 *
 * @param dateTime
 *            an org.joda.time.DateTime instance
 * @return true if the date is a "transition day"; false otherwise
 */
public static boolean isExtraHourForHourEnding(final DateTime dateTime) {
    boolean result = false;
    final DateTime twoHoursBefore = dateTime.minusHours(2);
    final DateTime oneHourBefore = dateTime.minusHours(1);
    final DateTime currentDate = dateTime;

    if (!marketTimeZone.isStandardOffset(twoHoursBefore.getMillis())
            && marketTimeZone.isStandardOffset(oneHourBefore.getMillis())
            && marketTimeZone.isStandardOffset(currentDate.getMillis())) {
        result = true;
    }

    return result;
}

From source file:org.vaadin.spring.samples.mvp.util.SSTimeUtil.java

License:Apache License

/**
 * Determines whether current hour is an "extra hour" when transitioning
 * from Daylight Savings to Standard Time for a DateTime that represents
 * hour ending data./*from  w  w w. j  a v  a  2s  .c  o  m*/
 *
 * @param dateTime
 *            an org.joda.time.DateTime instance
 * @return true if the date is a "transition day"; false otherwise
 */
public static boolean isExtraHourForHourEnding(final String isoDateTime) {
    DateTime dateTime = SSTimeUtil.isoToDateTime(isoDateTime);
    boolean result = false;
    final DateTime twoHoursBefore = dateTime.minusHours(2);
    final DateTime oneHourBefore = dateTime.minusHours(1);
    final DateTime currentDate = dateTime;

    if (!marketTimeZone.isStandardOffset(twoHoursBefore.getMillis())
            && marketTimeZone.isStandardOffset(oneHourBefore.getMillis())
            && marketTimeZone.isStandardOffset(currentDate.getMillis())) {
        result = true;
    }

    return result;
}

From source file:org.vaadin.spring.samples.mvp.util.SSTimeUtil.java

License:Apache License

public static boolean isExtraHour(final DateTime dateTime) {
    boolean result = false;
    final DateTime oneHourBefore = dateTime.minusHours(1);
    final DateTime currentDate = dateTime;

    if (!marketTimeZone.isStandardOffset(oneHourBefore.getMillis())
            && marketTimeZone.isStandardOffset(currentDate.getMillis())) {
        result = true;/*  ww w  . ja  va2  s  . c o  m*/
    }
    return result;
}

From source file:org.wildfly.metrics.server.Server.java

License:Apache License

public static void main(String[] args) throws Exception {
    final Server server = new Server();

    Undertow http = Undertow.builder().addHttpListener(4444, "localhost")
            .setHandler(path().addPrefixPath("/api/put", exchange -> {
                Map<String, Deque<String>> params = exchange.getQueryParameters();

                server.writeMetric(params.get("metric").getFirst(),
                        Long.valueOf(params.get("value").getFirst()));
            })/*  w  w  w  . ja  v  a 2  s .c  o  m*/

                    .addPrefixPath("/api/get", exchange -> {
                        Map<String, Deque<String>> params = exchange.getQueryParameters();

                        List<Long[]> values = server.readSlice(params.get("metric").getFirst(), () -> {
                            return new long[] { 0, System.currentTimeMillis() };
                        });

                        payload(exchange, params, values);

                    })

                    .addPrefixPath("/api/get15s", exchange -> {
                        Map<String, Deque<String>> params = exchange.getQueryParameters();

                        List<Long[]> values = server.readSlice(params.get("metric").getFirst(), () -> {
                            DateTime dt = new DateTime();
                            DateTime offset = dt.minusSeconds(15);
                            return new long[] { offset.getMillis(), System.currentTimeMillis() };
                        });

                        payload(exchange, params, values);
                    })

                    .addPrefixPath("/api/get30s", exchange -> {
                        Map<String, Deque<String>> params = exchange.getQueryParameters();

                        List<Long[]> values = server.readSlice(params.get("metric").getFirst(), () -> {
                            DateTime dt = new DateTime();
                            DateTime offset = dt.minusSeconds(30);
                            return new long[] { offset.getMillis(), System.currentTimeMillis() };
                        });

                        payload(exchange, params, values);
                    })

                    .addPrefixPath("/api/get1m", exchange -> {
                        Map<String, Deque<String>> params = exchange.getQueryParameters();

                        List<Long[]> values = server.readSlice(params.get("metric").getFirst(), () -> {
                            DateTime dt = new DateTime();
                            DateTime offset = dt.minusMinutes(1);
                            return new long[] { offset.getMillis(), System.currentTimeMillis() };
                        });

                        payload(exchange, params, values);
                    })

                    .addPrefixPath("/api/get1h", exchange -> {
                        Map<String, Deque<String>> params = exchange.getQueryParameters();

                        List<Long[]> values = server.readSlice(params.get("metric").getFirst(), () -> {
                            DateTime dt = new DateTime();
                            DateTime offset = dt.minusHours(1);
                            return new long[] { offset.getMillis(), System.currentTimeMillis() };
                        });

                        payload(exchange, params, values);
                    })

                    .addPrefixPath("/", exchange -> {
                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                        exchange.getResponseSender().send("Metric Server");
                    }))
            .build();

    server.start();
    http.start();

    System.out.println("Press enter to exit ...");
    System.in.read();

    http.stop();
    server.stop();
}

From source file:org.wso2.das4esb.integration.common.clients.ConcurrentEventsPublisher.java

License:Open Source License

@Override
public void run() {
    Object[] metaData = { true, tenantId };
    int sentFaults = 0;
    int publishedRequests = 0;
    // Publish events
    try {/* ww w . j  a  v  a2s .  c o m*/
        for (int j = 0; j < this.noOfRequests; j++) {
            boolean isFault = false;
            String messageId = "urn_uuid_" + UUID.randomUUID();
            String[] payloadData = new String[2];
            Event event;
            payloadData[0] = messageId;

            DateTime time = new DateTime();
            if (j < this.noOfRequests / 20) {
                time = time.minusMonths(3).minusDays(1).minusHours(2);
            } else if (j < this.noOfRequests / 20 * 2) {
                time = time.minusMonths(3).minusDays(1);
            } else if (j < this.noOfRequests / 20 * 3) {
                time = time.minusMonths(3);
            } else if (j < this.noOfRequests / 20 * 4) {
                time = time.minusMonths(2).minusDays(1).minusHours(2);
            } else if (j < this.noOfRequests / 20 * 5) {
                time = time.minusMonths(2).minusDays(1);
            } else if (j < this.noOfRequests / 20 * 6) {
                time = time.minusMonths(2);
            } else if (j < this.noOfRequests / 20 * 7) {
                time = time.minusMonths(1).minusDays(1).minusHours(2);
            } else if (j < this.noOfRequests / 20 * 8) {
                time = time.minusMonths(1).minusDays(1);
            } else if (j < this.noOfRequests / 20 * 9) {
                time = time.minusMonths(1);
            } else if (j < this.noOfRequests / 20 * 10) {
                time = time.minusHours(5);
            } else if (j < this.noOfRequests / 20 * 11) {
                time = time.minusHours(3);
            } else if (j < this.noOfRequests / 20 * 12) {
                time = time.minusHours(1);
            } else {
                isFault = sentFaults < this.noOfFaults;
            }

            long timeInMilliSec = time.getMillis();
            payloadData[1] = Utils.getESBCompressedEventString(messageId, this.entryPointName,
                    this.noOfMediators, this.payloadsEnabled, this.propertiesEnabled, isFault, timeInMilliSec);
            event = new Event(null, System.currentTimeMillis(), metaData, null, payloadData);
            this.dataPublisherClient.publish(TestConstants.ESB_FLOW_ENTRY_STREAM_NAME, "1.0.0", event);
            if (isFault) {
                sentFaults++;
            }
            // sleep to control the throughput
            Thread.sleep(this.sleepBetweenRequests);
            publishedRequests++;
        }
    } catch (DataEndpointException e) {
        throw new RuntimeException("Falied to publish event: " + e.getMessage(), e);
    } catch (InterruptedException ignored) {
    } finally {
        try {
            log.info("Published: " + publishedRequests + " events to: " + entryPointName + " for tenant: "
                    + tenantId);
            if (this.dataPublisherClient != null) {
                Thread.sleep(20000);
                this.dataPublisherClient.shutdown();
            }
        } catch (Exception ignored) {
        }
    }
}

From source file:proxi.model.DataFixer.java

License:Open Source License

private static DateTime paisCommentaryDateText2DateTime(Commentary commentary) {
    DateTime dt = new DateTime();
    String date = commentary.getDate().toLowerCase(); // .replaceAll(" - ",
    // " ").replaceAll(":",
    // " ");/*from   ww w. j  ava2s.co  m*/
    if (date.contains("menos de")) {
        return dt;
    } else if (date.contains("minutos") || date.contains("minuto")) {
        String[] dateParts = date.split(" ");
        int minutes = Integer.parseInt(dateParts[1]);
        return dt.minusMinutes(minutes);
    } else if (date.contains("horas") || date.contains("hora")) {
        String[] dateParts = date.split(" ");
        int hours = Integer.parseInt(dateParts[1]);
        return dt.minusHours(hours);
    }
    return null;
}