Example usage for org.joda.time DateTimeZone forOffsetHoursMinutes

List of usage examples for org.joda.time DateTimeZone forOffsetHoursMinutes

Introduction

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

Prototype

public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset)
        throws IllegalArgumentException 

Source Link

Document

Gets a time zone instance for the specified offset to UTC in hours and minutes.

Usage

From source file:com.foundationdb.server.types.mcompat.mtypes.MDateAndTime.java

License:Open Source License

/** Parse an hour:min or named timezone. */
public static DateTimeZone parseTimeZone(String tz) {
    try {//from   www . jav a 2 s  . com
        Matcher m = TZ_PATTERN.matcher(tz);
        if (m.matches()) {
            int hourSign = "-".equals(m.group(TZ_SIGN_GROUP)) ? -1 : 1;
            int hour = Integer.parseInt(m.group(TZ_HOUR_GROUP));
            int min = Integer.parseInt(m.group(TZ_MINUTE_GROUP));
            return DateTimeZone.forOffsetHoursMinutes(hourSign * hour, min);
        } else {
            // Upper 'utc' but not 'America/New_York'
            if (tz.indexOf('/') == -1) {
                tz = tz.toUpperCase();
            }
            return DateTimeZone.forID(tz);
        }
    } catch (IllegalArgumentException e) {
        throw new InvalidDateFormatException("timezone", tz);
    }
}

From source file:com.moosemorals.weather.xml.WeatherParser.java

License:Open Source License

protected DateTime readTimeZone(XMLStreamReader parser) throws XMLStreamException, IOException {
    parser.require(XMLStreamReader.START_ELEMENT, NAMESPACE, "time_zone");

    String rawDate = null;/*from  www .  j a  v a  2  s .c  o  m*/
    float utcOffset = 0.0f;

    while (parser.next() != XMLStreamReader.END_ELEMENT) {
        if (parser.getEventType() != XMLStreamReader.START_ELEMENT) {
            continue;
        }

        String raw;
        switch (parser.getLocalName()) {
        case "localtime":
            rawDate = readTag(parser, "localtime");
            break;
        case "utcOffset":
            utcOffset = readFloatTag(parser, "utcOffset");
            break;
        }
    }

    int hoursOffset = (int) utcOffset;
    int minutesOffset = (int) ((utcOffset - hoursOffset) * 60.0);

    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")
            .withZone(DateTimeZone.forOffsetHoursMinutes(hoursOffset, minutesOffset));

    return fmt.parseDateTime(rawDate);
}

From source file:com.thoughtworks.studios.journey.utils.TimeUtils.java

License:Open Source License

public static DateTimeZone timeZoneByMinutesOffset(int tzOffsetInMinutes) {
    int hours = tzOffsetInMinutes / 60;
    int minutes = tzOffsetInMinutes % 60;
    return DateTimeZone.forOffsetHoursMinutes(hours, minutes);
}

From source file:com.wso2telco.dep.reportingservice.dao.BillingDAO.java

License:Open Source License

/**
 * Convert to local time./*from  ww  w .  j a  va  2  s.  c o m*/
 *
 * @param timeOffset the time offset
 * @param time the time
 * @return the string
 */
public String convertToLocalTime(String timeOffset, String time) {
    Integer offsetValue = Integer.parseInt(timeOffset);

    log.debug("Offset value = " + offsetValue);
    DateTimeZone systemTimeZone = DateTimeZone.getDefault();
    log.debug("system time zone " + systemTimeZone.toString());
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    DateTime systemDateTime = formatter.parseDateTime(time);
    log.debug("system date time " + systemDateTime.toString());
    systemDateTime = systemDateTime.withZoneRetainFields(systemTimeZone);
    log.debug("system date time after adding systemtimezone === " + systemDateTime.toString());

    int hours = -1 * offsetValue / 60;
    int minutes = offsetValue % 60;
    minutes = Math.abs(minutes);

    DateTimeZone localTimeZone = DateTimeZone.forOffsetHoursMinutes(hours, minutes);

    log.debug("Local time zone ==== " + localTimeZone.toString());

    DateTime convertedDateTime = systemDateTime.withZone(localTimeZone);

    String convertedDateTimeString = formatter.print(convertedDateTime);

    log.debug("converted time  :" + convertedDateTimeString);

    return convertedDateTimeString;

}

From source file:com.xpn.xwiki.plugin.jodatime.JodaTimePlugin.java

License:Open Source License

/**
 * @see org.joda.time.DateTimeZone#forOffsetHoursMinutes(int, int)
 *///from  w  ww  . j  ava2 s  . co m
public DateTimeZone getTimezone(int offsetHours, int offsetMinutes) {
    return DateTimeZone.forOffsetHoursMinutes(offsetHours, offsetMinutes);
}

From source file:io.crate.expression.scalar.TimeZoneParser.java

License:Apache License

public static DateTimeZone parseTimeZone(BytesRef timezone) throws IllegalArgumentException {
    if (timezone == null) {
        throw new IllegalArgumentException("invalid time zone value NULL");
    }/* w  ww.  j av  a2 s . c o m*/
    if (timezone.equals(DEFAULT_TZ_BYTES_REF)) {
        return DEFAULT_TZ;
    }

    DateTimeZone tz = TIME_ZONE_MAP.get(timezone);
    if (tz == null) {
        try {
            String text = timezone.utf8ToString();
            int index = text.indexOf(':');
            if (index != -1) {
                int beginIndex = text.charAt(0) == '+' ? 1 : 0;
                // format like -02:30
                tz = DateTimeZone.forOffsetHoursMinutes(Integer.parseInt(text.substring(beginIndex, index)),
                        Integer.parseInt(text.substring(index + 1)));
            } else {
                // id, listed here: http://joda-time.sourceforge.net/timezones.html
                // or here: http://www.joda.org/joda-time/timezones.html
                tz = DateTimeZone.forID(text);
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(
                    String.format(Locale.ENGLISH, "invalid time zone value '%s'", timezone.utf8ToString()));
        }
        TIME_ZONE_MAP.putIfAbsent(timezone, tz);
    }
    return tz;
}

From source file:io.crate.operation.scalar.DateTruncTimeZoneAwareFunction.java

License:Apache License

private DateTimeZone parseZone(BytesRef zone) throws IllegalArgumentException {
    String text = zone.utf8ToString();
    int index = text.indexOf(':');
    if (index != -1) {
        int beginIndex = text.charAt(0) == '+' ? 1 : 0;
        // format like -02:30
        return DateTimeZone.forOffsetHoursMinutes(Integer.parseInt(text.substring(beginIndex, index)),
                Integer.parseInt(text.substring(index + 1)));
    } else {/*w  w w  .  j  a  v  a 2  s . c om*/
        // id, listed here: http://joda-time.sourceforge.net/timezones.html
        // or here: http://www.joda.org/joda-time/timezones.html
        return DateTimeZone.forID(text);
    }
}

From source file:nz.al4.airclock.TimeCalculator.java

License:Open Source License

/**
 * Get date time zone object for current point of flight
 * @return//from  w  ww .java2s. c o  m
 */
public DateTimeZone getTimeZone() {
    int offsetMins = (int) getEffectiveOffsetMinutes();
    Integer[] offsetHoursMinutes = minutesToHoursMinutes(offsetMins);
    Integer offsetHours = offsetHoursMinutes[0];
    Integer offsetMinutes = offsetHoursMinutes[1];

    return DateTimeZone.forOffsetHoursMinutes(offsetHours, offsetMinutes);
}

From source file:nz.co.jsrsolutions.tideservice.scraper.provider.EasyTideTideDataProvider.java

License:Open Source License

@Override
public List<TidePredictionDay> getTidePredictionDay(Port port) throws TideDataProviderException {

    HttpGet getRequest;//w w  w .j a v a 2  s  .c  o m
    try {

        getRequest = new EasyTideHttpGet(
                mUriBuilder.buildGetPredictionUri(mGetTidePredictionUrlSuffix, port.getExternalId(), 3));
        HttpResponse response = mHttpClient.execute(getRequest);

        Parser parser = getHtmlParser(response);

        // Parse the timezone information
        NodeList nodeList = new NodeList();
        NodeFilter filter = new AndFilter(new TagNameFilter("SPAN"),
                new HasAttributeFilter("ID", "PredictionSummary1_lblZoneTimeOffset"));

        parser.reset();
        for (NodeIterator e = parser.elements(); e.hasMoreNodes();) {
            e.nextNode().collectInto(nodeList, filter);
        }

        if (nodeList.size() != 1) {
            throw new TideDataProviderException("Couldn't retrieve the time zone information");
        }

        String timeZoneString = ((TagNode) nodeList.elementAt(0)).getChildren().elementAt(0).getText();
        Matcher timeZoneMatcher = mTimeZoneRegexPattern.matcher(timeZoneString);

        // attempt the common case first
        int hoursOffset;
        int minutesOffset;
        if (timeZoneMatcher.matches()) {

            hoursOffset = Integer.parseInt(timeZoneMatcher.group(1));
            final String minutesString = timeZoneMatcher.group(3);
            minutesOffset = (minutesString == null) ? 0 : Integer.parseInt(minutesString);

        } else if (timeZoneString.compareTo("Port predictions (Standard Local Time) are equal to UTC") == 0) {
            // is already UTC
            hoursOffset = 0;
            minutesOffset = 0;
        } else {
            throw new TideDataProviderException(
                    "Couldn't parse the time zone information from: ".concat(timeZoneString));
        }
        final DateTimeZone timeZone = DateTimeZone.forOffsetHoursMinutes(hoursOffset, minutesOffset);

        // Parse the current day (today) information
        nodeList = new NodeList();
        filter = new AndFilter(new TagNameFilter("SPAN"),
                new HasAttributeFilter("ID", "PredictionSummary1_lblPredictionStart"));

        parser.reset();
        for (NodeIterator e = parser.elements(); e.hasMoreNodes();) {
            e.nextNode().collectInto(nodeList, filter);
        }

        if (nodeList.size() != 1) {
            throw new TideDataProviderException("Couldn't retrieve today's date");
        }

        String todayString = ((TagNode) nodeList.elementAt(0)).getChildren().elementAt(0).getText();
        Matcher todayMatcher = mTodayRegexPattern.matcher(todayString);

        LocalDate localDate;
        if (todayMatcher.matches()) {
            localDate = LocalDate.parse(todayMatcher.group(1).concat(todayMatcher.group(2)),
                    mLocalDateFormatter);
        } else {
            throw new TideDataProviderException(
                    "Couldn't parse the time zone information from: ".concat(timeZoneString));
        }

        // Get each of the HW,LW tables
        nodeList = new NodeList();
        filter = new AndFilter(new TagNameFilter("TABLE"),
                new OrFilter(new HasAttributeFilter("CLASS", "HWLWTable"),
                        new OrFilter(new HasAttributeFilter("CLASS", "HWLWTable first"),
                                new OrFilter(new HasAttributeFilter("CLASS", "HWLWTable last"),
                                        new HasAttributeFilter("CLASS", "HWLWTable first last")))));

        parser.reset();
        for (NodeIterator e = parser.elements(); e.hasMoreNodes();) {
            e.nextNode().collectInto(nodeList, filter);
        }

        int numDays = nodeList.size();
        final List<TidePredictionDay> tidePredictionDays = new ArrayList<TidePredictionDay>(numDays);

        for (int nDay = 0; nDay < numDays; ++nDay) {

            final TagNode tagNode = (TagNode) nodeList.elementAt(nDay);

            final TidePredictionDay tidePredictionDay = new TidePredictionDay();
            tidePredictionDay.setLocalDate(localDate);

            // LWHW
            NodeList lwHwNodeList = new NodeList();
            filter = new AndFilter(new TagNameFilter("TH"),
                    new HasAttributeFilter("CLASS", "HWLWTableHWLWCellPrintFriendly"));
            tagNode.collectInto(lwHwNodeList, filter);

            // Times and Heights
            NodeList timeHeightNodeList = new NodeList();
            filter = new AndFilter(new TagNameFilter("TD"),
                    new HasAttributeFilter("CLASS", "HWLWTableCellPrintFriendly"));
            tagNode.collectInto(timeHeightNodeList, filter);

            int numTides = lwHwNodeList.size();

            for (int nTide = 0; nTide < numTides; ++nTide) {
                final TidePrediction tidePrediction = new TidePrediction();

                tidePrediction.setTidePredictionType(TidePredictionType
                        .fromString(lwHwNodeList.elementAt(nTide).getChildren().elementAt(0).getText()));

                // Set the time
                LocalTime localTime;
                String localTimeString = timeHeightNodeList.elementAt(nTide).getChildren().elementAt(0)
                        .getText();
                if (localTimeString != null && !localTimeString.isEmpty()
                        && localTimeString.compareTo("&nbsp;") != 0) {
                    if (localTimeString.contains("*")) {
                        localTimeString = localTimeString.replace("*", "");
                        tidePrediction.setIsEstimate(true);
                    }
                    localTime = mLocalTimeFormatter.parseLocalTime(localTimeString);
                } else {
                    // we can't really make out that this is a sensible prediction
                    // so don't include this
                    continue;
                }

                final DateTime dateTime = localDate.toDateTime(localTime, timeZone);
                tidePrediction.setUtcTime(dateTime);

                // Set the height where possible
                final String heightString = timeHeightNodeList.elementAt(nTide + numTides).getChildren()
                        .elementAt(0).getText();

                if (heightString != null && !heightString.isEmpty()) {

                    Matcher tideHeightMatcher = mTideHeightRegexPattern.matcher(heightString);
                    if (tideHeightMatcher.matches()) {
                        tidePrediction.setHeight(Float.parseFloat(tideHeightMatcher.group(1)));
                    }

                }

                // Tie both together
                tidePrediction.setTidePredictionDay(tidePredictionDay);
                tidePredictionDay.getTidePredictions().add(tidePrediction);
            }

            tidePredictionDays.add(tidePredictionDay);
            localDate = localDate.plusDays(1);
        }

        return tidePredictionDays;

    } catch (URISyntaxException e) {
        throw new TideDataProviderException(e);
    } catch (IOException e) {
        throw new TideDataProviderException(e);
    } catch (ParseException e) {
        throw new TideDataProviderException(e);
    } catch (ParserException e) {
        throw new TideDataProviderException(e);
    }

}

From source file:org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramParser.java

License:Apache License

private DateTimeZone parseZone(String text) throws IOException {
    int index = text.indexOf(':');
    if (index != -1) {
        int beginIndex = text.charAt(0) == '+' ? 1 : 0;
        // format like -02:30
        return DateTimeZone.forOffsetHoursMinutes(Integer.parseInt(text.substring(beginIndex, index)),
                Integer.parseInt(text.substring(index + 1)));
    } else {/*from  w  ww. java2  s . co  m*/
        // id, listed here: http://joda-time.sourceforge.net/timezones.html
        return DateTimeZone.forID(text);
    }
}