Example usage for org.joda.time DateTimeZone forTimeZone

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

Introduction

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

Prototype

public static DateTimeZone forTimeZone(TimeZone zone) 

Source Link

Document

Gets a time zone instance for a JDK TimeZone.

Usage

From source file:org.obm.push.RecurrenceDayOfWeekConverter.java

License:Open Source License

public static RecurrenceDays byUTCDate(Date date, TimeZone tz) {
    int jodaDateOfWeek = new DateTime(date, DateTimeZone.UTC).withZone(DateTimeZone.forTimeZone(tz))
            .getDayOfWeek();//from w  w  w  . j  a v  a2  s  .  c om

    return JODA_TO_DAYS.get(jodaDateOfWeek);
}

From source file:org.ohmage.domain.campaign.prompt.TimestampPrompt.java

License:Apache License

/**
 * Validates that a given value is valid and, if so, converts it into an
 * appropriate object./* ww  w.  j  av a2  s  .com*/
 * 
 * @param value The value to be validated. This must be one of the  
 *             following:<br />
 *             <ul>
 *             <li>{@link NoResponse}</li>
 *             <li>{@link Date}</li>
 *             <li>{@link Calendar}</li>
 *             <li>{@link String} that represents:</li>
 *               <ul>
 *                 <li>{@link NoResponse}</li>
 *                 <li>ISO 8601 formatted date with or without the time.
 *                   </li>
 *               <ul>
 *             </ul>
 * 
 * @return A Date object or a {@link NoResponse} object.
 * 
 * @throws DomainException The value is invalid.
 */
@Override
public Object validateValue(final Object value) throws DomainException {
    // If it's already a NoResponse value, then return make sure that if it
    // was skipped that it as skippable.
    if (value instanceof NoResponse) {
        if (NoResponse.SKIPPED.equals(value) && (!skippable())) {
            throw new DomainException("The prompt, '" + getId() + "', was skipped, but it is not skippable.");
        }

        return value;
    }
    // If it's already a DateTime, return it.
    else if (value instanceof DateTime) {
        return value;
    }
    // If it's a Date, convert it to a DateTime and return it.
    else if (value instanceof Date) {
        return new DateTime(((Date) value).getTime());
    }
    // If it's a Calendar, convert it to a DateTime and return it.
    else if (value instanceof Calendar) {
        Calendar calValue = (Calendar) value;
        return new DateTime(calValue.getTimeInMillis(), DateTimeZone.forTimeZone(calValue.getTimeZone()));
    }
    // If it's a String, attempt to convert it to a DateTime and return it.
    else if (value instanceof String) {
        try {
            return NoResponse.valueOf((String) value);
        } catch (IllegalArgumentException notNoResponse) {
            try {
                return DateTimeUtils.getDateTimeFromString((String) value);
            } catch (IllegalArgumentException notOurDateTime) {
                try {
                    return ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime((String) value);
                } catch (IllegalArgumentException notIsoDateTime) {
                    throw new DomainException(
                            "The string value could not be converted to a date for prompt '" + getId() + "'.",
                            notIsoDateTime);
                }
            }
        }
    }

    throw new DomainException("The value could not be converted to a valid date for prompt '" + getId() + "'.");
}

From source file:org.ohmage.streams.StreamPointBuilder.java

License:Apache License

/**
 * Set the time and timezone for this string. Time Zone is required for correct visualization of
 * the data point.// w w w. j a v a 2s.c  om
 *
 * @param time
 * @param timeZone
 * @return
 */
public StreamPointBuilder withTime(Date time, TimeZone timeZone) {
    if (dateTimeFormatter == null)
        dateTimeFormatter = ISODateTimeFormat.dateTime().withOffsetParsed();
    mTimestamp = dateTimeFormatter.print(new DateTime(time, DateTimeZone.forTimeZone(timeZone)));
    return this;
}

From source file:org.opendatakit.common.android.utilities.DataUtil.java

License:Apache License

public DataUtil(Locale locale, TimeZone tz) {
    this.locale = locale;
    this.tz = DateTimeZone.forTimeZone(tz);
    DateTimeFormatterBuilder fpBuilder = new DateTimeFormatterBuilder();
    for (String pattern : USER_FULL_DATETIME_PATTERNS) {
        DateTimeFormatter f = DateTimeFormat.forPattern(pattern);
        fpBuilder.appendOptional(f.getParser());
    }/* w ww.jav  a  2  s.  c  o m*/
    userFullParser = fpBuilder.toFormatter().withLocale(locale).withZone(this.tz);
    userPartialParsers = new DateTimeFormatter[USER_PARTIAL_DATETIME_PATTERNS.length];
    for (int i = 0; i < USER_PARTIAL_DATETIME_PATTERNS.length; i++) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        for (String pattern : USER_PARTIAL_DATETIME_PATTERNS[i]) {
            DateTimeFormatter f = DateTimeFormat.forPattern(pattern);
            dtfb.appendOptional(f.getParser());
        }
        userPartialParsers[i] = dtfb.toFormatter().withLocale(locale).withZone(this.tz);
    }
    userShortFormatter = DateTimeFormat.forPattern(USER_SHORT_FORMAT);
    userLongFormatter = DateTimeFormat.forPattern(USER_LONG_FORMAT);
}

From source file:org.opendatakit.tables.data.UserTable.java

License:Apache License

private void buildFormatters() {
    Locale l = Locale.getDefault();
    tz = DateTimeZone.forTimeZone(TimeZone.getDefault());
    dateFormatter = DateTimeFormat.forPattern(DateTimeFormat.patternForStyle("M-", l)).withZone(tz);
    dateTimeFormatter = DateTimeFormat.forPattern(DateTimeFormat.patternForStyle("ML", l)).withZone(tz);
    timeFormatter = DateTimeFormat.forPattern(DateTimeFormat.patternForStyle("-L", l)).withZone(tz);
}

From source file:org.opendatakit.utilities.DateUtils.java

License:Apache License

public DateUtils(Locale locale, TimeZone tz) {
    this.locale = locale;
    DateTimeZone timeZone = DateTimeZone.forTimeZone(tz);
    DateTimeFormatterBuilder fpBuilder = new DateTimeFormatterBuilder();
    for (String pattern : USER_FULL_DATETIME_PATTERNS) {
        DateTimeFormatter f = DateTimeFormat.forPattern(pattern);
        fpBuilder.appendOptional(f.getParser());
    }//from  w ww.ja v  a2s  . co  m
    userFullParser = fpBuilder.toFormatter().withLocale(locale).withZone(timeZone);
    userPartialParsers = new DateTimeFormatter[USER_PARTIAL_DATETIME_PATTERNS.length];
    for (int i = 0; i < USER_PARTIAL_DATETIME_PATTERNS.length; i++) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        for (String pattern : USER_PARTIAL_DATETIME_PATTERNS[i]) {
            DateTimeFormatter f = DateTimeFormat.forPattern(pattern);
            dtfb.appendOptional(f.getParser());
        }
        userPartialParsers[i] = dtfb.toFormatter().withLocale(locale).withZone(timeZone);
    }
}

From source file:org.openehr.rm.datatypes.quantity.datetime.DvDateTimeParser.java

License:LGPL

public static DateTime convertTime(int hour, int minute, int second, double fractSec, TimeZone timezone) {
    if (fractSec > 1 || fractSec < 0) {
        throw new IllegalArgumentException("invalid fractional second");
    }//from w ww . j  a  v  a  2 s  .c  o m
    return new DateTime(1970, 1, 1, hour, minute, second, (int) (fractSec * 1000),
            DateTimeZone.forTimeZone(timezone));
}

From source file:org.openehr.rm.datatypes.quantity.datetime.DvDateTimeParser.java

License:LGPL

public static DateTime convertDateTime(int year, int month, int day, int hour, int minute, int second,
        double fractSec, TimeZone timezone) {
    if (fractSec > 1) {
        throw new IllegalArgumentException("invalid fractional second");
    }/*  w  w  w .j  a  v  a2 s .  c o m*/
    return new DateTime(year, month, day, hour, minute, second, (int) (fractSec * 1000),
            DateTimeZone.forTimeZone(timezone));
}

From source file:org.pidome.misc.utils.TimeUtils.java

License:Apache License

/**
 * Sets new date and time//w  w  w  .  ja va 2s  .c om
 */
private static void setDateTime() {
    dt = new DateTime(DateTimeZone.forTimeZone(TimeZone.getDefault()));
    String realCurDate = composeDDMMYYYYDate(dt.getDayOfMonth(), dt.getMonthOfYear(), dt.getYear());
    if (!internalCurDate.equals(realCurDate)) {
        TimeUtils.setNewLocalizedValues();
        internalCurDate = realCurDate;
    }
}

From source file:org.pidome.plugins.weatherplugins.pidomeOpenWeatherMap.PidomeOpenWeatherMap.java

/**
 * Creates the weather data./*from ww w  . j a v  a2 s.  c  o m*/
 * @param rootData
 * @return 
 */
private CurrentWeatherData createWeatherData(Map<String, Object> rootData, boolean dayData) {
    CurrentWeatherData data = new CurrentWeatherData();
    try {
        Map<String, Object> mainData = (Map<String, Object>) rootData.get("main");
        long weatherTime = new DateTime(((Long) rootData.get("dt")) * 1000L,
                DateTimeZone.forTimeZone(TimeZone.getTimeZone("Etc/GMT")))
                        .toDateTime(DateTimeZone.forTimeZone(SharedServerTimeService.getCurrentTimeZone()))
                        .getMillis();
        boolean night = false;
        if (dayData) {
            if (weatherTime > SharedServerTimeService.getSunset()
                    || weatherTime < SharedServerTimeService.getSunrise()) {
                night = true;
            }
        }
        data.setWeatherDate((int) (weatherTime / 1000));
        data.setDate(new Date());
        data.setTemperature(
                Float.valueOf(tempFormat.format(((Number) mainData.get("temp")).doubleValue() - 273.15d)));
        data.setHumidity(Integer.parseInt(mainData.get("humidity").toString()));
        data.setPressure(Float.parseFloat(mainData.get("pressure").toString()));

        /// Just in case it is not available
        try {
            Map<String, Object> weatherData = ((List<Map<String, Object>>) rootData.get("weather")).get(0);
            data.setStateIcon(getWeatherIcon(((Number) weatherData.get("id")).intValue(), night));
            if (weatherData.containsKey("description")) {
                data.setStateName((String) weatherData.get("description"));
            } else {
                data.setStateName((String) weatherData.get("main"));
            }
            /// in case there are multiple weather definitions will be looking for extremes.
            try {
                ((List<Map<String, Object>>) rootData.get("weather")).remove(0);
                for (Map<String, Object> extremesData : ((List<Map<String, Object>>) rootData.get("weather"))) {
                    switch (((Number) weatherData.get("id")).intValue()) {
                    case 900:
                    case 901:
                    case 902:
                    case 903:
                    case 904:
                    case 905:
                    case 906:
                        data.addExtremesDescription((String) extremesData.get("description"));
                        break;
                    }
                }
            } catch (Exception ex) {
                //// Fail silently
            }
            /// Just in case wind is not available
            try {
                Map<String, Object> windData = (Map<String, Object>) rootData.get("wind");
                data.setWindSpeed(((Number) windData.get("speed")).floatValue());
                data.setWindDirection(
                        WeatherData.getWindDirection(((Number) windData.get("deg")).floatValue()));
                data.setWindDirectionDegrees(((Number) windData.get("deg")).floatValue());
            } catch (Exception ex) {
                LOG.warn("No wind info available: {}", ex.getMessage());
            }
        } catch (Exception ex) {
            LOG.warn("No basic weather info available: {}", ex.getMessage());
        }
    } catch (Exception ex) {
        LOG.warn("Could not compose weather data: {}, Data: {}", ex.getMessage(), rootData, ex);
    }
    return data;
}