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.jasig.portlet.cms.controller.EditPostController.java

License:Apache License

private void savePost(final ActionRequest request, final BindingResult result, Post post,
        boolean postIsScheduled, String scheduledDate, boolean removeExistingPost)
        throws PortletRequestBindingException, JcrRepositoryException {

    final PortletPreferencesWrapper pref = new PortletPreferencesWrapper(request);
    final Calendar cldr = Calendar.getInstance(request.getLocale());

    final DateTimeZone zone = DateTimeZone.forTimeZone(cldr.getTimeZone());
    final DateTime today = new DateTime(zone);

    final DateTimeFormatter fmt = DateTimeFormat.forPattern(PortletPreferencesWrapper.DEFAULT_POST_DATE_FORMAT);
    post.setLastModifiedDate(today.toString(fmt));

    post.setLanguage(request.getLocale().getLanguage());

    if (postIsScheduled) {
        if (StringUtils.isBlank(scheduledDate))
            result.rejectValue("scheduledDate", "invalid.scheduled.post.publish.date");
        else {//from w ww  .  j a v a2  s. c  om

            logDebug("Post is scheduled to be published on " + scheduledDate);
            final DateTime dt = DateTime.parse(scheduledDate, fmt);
            post.setScheduledDate(dt.toString(fmt));

            if (removeExistingPost) {
                if (getRepositoryDao().exists(post.getPath())) {
                    logDebug("Preparing scheduled post. Removing existing post at " + post.getPath());
                    getRepositoryDao().removePost(post.getPath());
                }
            }

            getRepositoryDao().schedulePost(post, pref.getPortletRepositoryRoot());
            ensureRepositoryRootIsScheduled(pref);
        }
    } else {
        post = preparePost(post, request);

        getRepositoryDao().setPost(post);
    }
}

From source file:org.jasig.portlet.cms.model.repository.schedule.ScheduledPostPublisher.java

License:Apache License

private void publishScheduledPostsForRepositoryRoot(final String root) throws JcrRepositoryException {
    final Collection<Post> scheduledPosts = getRepositoryDao().getScheduledPosts(root);

    if (scheduledPosts != null && scheduledPosts.size() > 0)
        for (final Post post : scheduledPosts) {

            final Calendar cldr = Calendar.getInstance(post.getLocale());
            final DateTimeFormatter fmt = DateTimeFormat
                    .forPattern(PortletPreferencesWrapper.DEFAULT_POST_DATE_FORMAT);

            final DateTime dt = DateTime.parse(post.getScheduledDate(), fmt);

            final DateTimeZone zone = DateTimeZone.forTimeZone(cldr.getTimeZone());
            final DateTime today = new DateTime(zone);

            if (dt.isEqual(today) || dt.isBefore(today)) {
                if (logger.isDebugEnabled())
                    logger.debug("Scheduled for today " + post.getPath() + " at " + post.getScheduledDate());

                getRepositoryDao().removePost(post.getPath());
                post.setPath(root);// w w  w .ja va 2 s  .c o m
                post.setRateCount(0);
                post.setRate(0);
                getRepositoryDao().setPost(post);

                if (logger.isDebugEnabled())
                    logger.debug("Published scheduled post " + post.getPath() + " at " + post.getScheduledDate()
                            + " to " + post.getPath());
            }
        }
    else
        ScheduledPostsManager.getInstance().removeRepositoryRoot(root);
}

From source file:org.jdesktop.swingx.DateTimePicker.java

License:LGPL

public void setDate(DateTime d) {
    super.setDate(d.withZone(DateTimeZone.forTimeZone(getTimeZone())).toDate());
}

From source file:org.jruby.CompatVersion.java

License:LGPL

public static DateTimeZone getLocalTimeZone(Ruby runtime) {
        RubyString tzVar = runtime.newString(TZ_STRING);
        RubyHash h = ((RubyHash) runtime.getObject().fastGetConstant("ENV"));
        IRubyObject tz = h.op_aref(runtime.getCurrentContext(), tzVar);
        if (tz == null || !(tz instanceof RubyString)) {
            return DateTimeZone.getDefault();
        } else {//from   w  ww.  j a  v a 2  s.c  o  m
            String zone = tz.toString();
            DateTimeZone cachedZone = runtime.getLocalTimezoneCache().get(zone);

            if (cachedZone != null)
                return cachedZone;

            String originalZone = zone;

            // Value of "TZ" property is of a bit different format,
            // which confuses the Java's TimeZone.getTimeZone(id) method,
            // and so, we need to convert it.

            Matcher tzMatcher = TZ_PATTERN.matcher(zone);
            if (tzMatcher.matches()) {
                String sign = tzMatcher.group(2);
                String hours = tzMatcher.group(3);
                String minutes = tzMatcher.group(4);

                // GMT+00:00 --> Etc/GMT, see "MRI behavior"
                // comment below.
                if (("00".equals(hours) || "0".equals(hours))
                        && (minutes == null || ":00".equals(minutes) || ":0".equals(minutes))) {
                    zone = "Etc/GMT";
                } else {
                    // Invert the sign, since TZ format and Java format
                    // use opposite signs, sigh... Also, Java API requires
                    // the sign to be always present, be it "+" or "-".
                    sign = ("-".equals(sign) ? "+" : "-");

                    // Always use "GMT" since that's required by Java API.
                    zone = "GMT" + sign + hours;

                    if (minutes != null) {
                        zone += minutes;
                    }
                }
            }

            // MRI behavior: With TZ equal to "GMT" or "UTC", Time.now
            // is *NOT* considered as a proper GMT/UTC time:
            //   ENV['TZ']="GMT"
            //   Time.now.gmt? ==> false
            //   ENV['TZ']="UTC"
            //   Time.now.utc? ==> false
            // Hence, we need to adjust for that.
            if ("GMT".equalsIgnoreCase(zone) || "UTC".equalsIgnoreCase(zone)) {
                zone = "Etc/" + zone;
            }

            DateTimeZone dtz = DateTimeZone.forTimeZone(TimeZone.getTimeZone(zone));
            runtime.getLocalTimezoneCache().put(originalZone, dtz);
            return dtz;
        }
    }

From source file:org.kalypso.commons.parser.impl.DateParser.java

License:Open Source License

public void setTimezone(final TimeZone tz) {
    if (tz == null)
        m_df = m_df.withZone(DateTimeZone.getDefault());
    else {/*from  w w  w  .ja v  a 2 s  . c  o  m*/
        final DateTimeZone zone = DateTimeZone.forTimeZone(tz);
        m_df = m_df.withZone(zone);
    }
}

From source file:org.kalypso.model.hydrology.internal.binding.cm.LinearSumGenerator.java

License:Open Source License

@Override
public void adjustValidities() {
    /* Get the valid from date and the valid to date. */
    final Date validFrom = getValidFrom();
    final Date validTo = getValidTo();
    if (validFrom == null || validTo == null)
        return;/*from  ww  w  .j av a  2  s.  c  om*/

    /* Get the valid from date. */
    final Calendar validFromCalendar = Calendar.getInstance(KalypsoCorePlugin.getDefault().getTimeZone());
    validFromCalendar.setTime(validFrom);

    /* Get the valid to date. */
    final Calendar validToCalendar = Calendar.getInstance(KalypsoCorePlugin.getDefault().getTimeZone());
    validToCalendar.setTime(validTo);

    /* Get the timestamp. */
    final LocalTime timestamp = getTimestamp();
    if (timestamp != null) {
        /* Convert to a date with the kalypso timezone. */
        /* The date fields are ignored. */
        final DateTime timestampUTC = timestamp
                .toDateTimeToday(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); //$NON-NLS-1$
        final DateTime timestampDate = new DateTime(timestampUTC.toDate(),
                DateTimeZone.forTimeZone(KalypsoCorePlugin.getDefault().getTimeZone()));

        /* With a timestamp adjust the hours and the minutes accordingly. */
        validFromCalendar.set(Calendar.HOUR_OF_DAY, timestampDate.getHourOfDay());
        validFromCalendar.set(Calendar.MINUTE, timestampDate.getMinuteOfHour());
        validToCalendar.set(Calendar.HOUR_OF_DAY, timestampDate.getHourOfDay());
        validToCalendar.set(Calendar.MINUTE, timestampDate.getMinuteOfHour());
    } else {
        /* Without a timestemp set the hours and the minutes to zero. */
        validFromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        validFromCalendar.set(Calendar.MINUTE, 0);
        validToCalendar.set(Calendar.HOUR_OF_DAY, 0);
        validToCalendar.set(Calendar.MINUTE, 0);
    }

    /* Always set the seconds and milliseconds to zero. */
    validFromCalendar.set(Calendar.SECOND, 0);
    validFromCalendar.set(Calendar.MILLISECOND, 0);
    validToCalendar.set(Calendar.SECOND, 0);
    validToCalendar.set(Calendar.MILLISECOND, 0);

    /* Set the properties. */
    setProperty(PROPERTY_VALID_FROM, DateUtilities.toXMLGregorianCalendar(validFromCalendar.getTime()));
    setProperty(PROPERTY_VALID_TO, DateUtilities.toXMLGregorianCalendar(validToCalendar.getTime()));
}

From source file:org.kalypso.ogc.sensor.filter.filters.interval.IntervalValuesOperation.java

License:Open Source License

private IntervalIndex buildSourceIndex() throws SensorException {
    final IntervalIndex index = new IntervalIndex();

    final IAxis[] axes = m_sourceModel.getAxes();
    final IAxis dateAxis = AxisUtils.findDateAxis(axes);

    final Period step = findStep();

    final DateTimeZone zone = DateTimeZone.forTimeZone(m_timeZone);

    for (int i = 0; i < m_sourceModel.size(); i++) {
        final Date date = (Date) m_sourceModel.get(i, dateAxis);

        final DateTime to = new DateTime(date, zone);
        final DateTime from = to.minus(step);
        final Interval sourceInterval = new Interval(from, to);

        final IntervalData sourceData = m_axes.asIntervalData(sourceInterval, m_sourceModel, i);
        index.insert(sourceData);/*from   ww w .  j  ava2  s.  com*/
    }

    return index;
}

From source file:org.kalypso.ogc.sensor.filter.filters.interval.IntervalValuesOperation.java

License:Open Source License

private IntervalIterator createTargetIterator(final DateRange range, final int calendarField,
        final int amount) {
    final DateRange adjustedRange = adjustRange(range);

    final DateTimeZone zone = DateTimeZone.forTimeZone(m_timeZone);

    final DateTime fromTime = new DateTime(adjustedRange.getFrom(), zone);
    final DateTime toTime = new DateTime(adjustedRange.getTo(), zone);

    final Period step = PeriodUtils.getPeriod(calendarField, amount);

    return new IntervalIterator(fromTime, toTime, step);
}

From source file:org.kalypso.ogc.sensor.util.TimestampHelper.java

License:Open Source License

/**
 * This function converts the timestamp text (e.g. 11:00) into the kalypso timezone.
 * // w ww. j  a v  a 2s .c  om
 * @param timestampText
 *          The timestamp text in UTC.
 * @return The timestamp text in the kalypso timezone.
 */
public static String convertToKalypsoTimezone(final String timestampText) {
    /* Nothing to do. */
    if (timestampText == null || timestampText.length() == 0)
        return ""; //$NON-NLS-1$

    /* Get the timestamp in UTC. */
    final LocalTime timestamp = TimestampHelper.parseTimestamp(timestampText);

    /* Convert to a date with the kalypso timezone. */
    /* The date fields are ignored. */
    final DateTime timestampUTC = timestamp
            .toDateTimeToday(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); //$NON-NLS-1$
    final DateTime timestampZone = new DateTime(timestampUTC.toDate(),
            DateTimeZone.forTimeZone(KalypsoCorePlugin.getDefault().getTimeZone()));

    return timestampZone.toString("HH:mm"); //$NON-NLS-1$
}

From source file:org.kalypso.ogc.sensor.util.TimestampHelper.java

License:Open Source License

/**
 * This function converts the timestamp text (e.g. 11:00) into UTC.
 * //from w  w w .j a  va  2  s  .  c  o  m
 * @param timestampText
 *          The timestamp text in the kalypso timezone.
 * @return The timestamp text in UTC.
 */
public static String convertToUTC(final String timestampText) {
    /* Nothing to do. */
    if (timestampText == null || timestampText.length() == 0)
        return ""; //$NON-NLS-1$

    /* Create the date time formatter. */
    final DateTimeFormatter formatter = createDateTimeFormatter();
    final DateTimeFormatter formatterZone = formatter
            .withZone(DateTimeZone.forTimeZone(KalypsoCorePlugin.getDefault().getTimeZone()));

    /* Get the timestamp in the kalypso timezone. */
    final DateTime timestampZone = formatterZone.parseDateTime(timestampText);
    final DateTime timestampUTC = new DateTime(timestampZone.toDate(),
            DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"))); //$NON-NLS-1$

    return timestampUTC.toString("HH:mm"); //$NON-NLS-1$
}