Example usage for org.joda.time.format DateTimeFormatter withZone

List of usage examples for org.joda.time.format DateTimeFormatter withZone

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatter withZone.

Prototype

public DateTimeFormatter withZone(DateTimeZone zone) 

Source Link

Document

Returns a new formatter that will use the specified zone in preference to the zone of the printed object, or default zone on a parse.

Usage

From source file:org.projectforge.framework.xstream.converter.JodaDateTimeConverter.java

License:Open Source License

String toString(final Object source) {
    final DateTime data = (DateTime) source;
    final DateTimeFormatter printFormat = DateTimeFormat.forPattern(ISO_FORMAT);
    final String date = printFormat.withZone(DateTimeZone.UTC).print(data);
    return date;//from   w  w  w.  ja va 2  s.  co  m
}

From source file:org.projectforge.framework.xstream.converter.JodaDateTimeConverter.java

License:Open Source License

Object parse(final String data) {
    try {/* w ww. ja v  a2 s.c o m*/
        final DateTimeFormatter parseFormat = DateTimeFormat.forPattern(ISO_FORMAT);
        final DateTime date = parseFormat.withZone(DateTimeZone.UTC).parseDateTime(data);
        final DateTimeZone dateTimeZone = ThreadLocalUserContext.getDateTimeZone();
        return new DateTime(date, dateTimeZone);
    } catch (final Exception ex) {
        log.error("Can't parse DateMidnight: " + data);
        return new DateMidnight();
    }
}

From source file:org.projectforge.xstream.JodaDateTimeConverter.java

License:Open Source License

Object parse(final String data) {
    try {/*from   w  ww  .ja v  a2 s.co m*/
        final DateTimeFormatter parseFormat = DateTimeFormat.forPattern(ISO_FORMAT);
        final DateTime date = parseFormat.withZone(DateTimeZone.UTC).parseDateTime(data);
        final DateTimeZone dateTimeZone = PFUserContext.getDateTimeZone();
        return new DateTime(date, dateTimeZone);
    } catch (final Exception ex) {
        log.error("Can't parse DateMidnight: " + data);
        return new DateMidnight();
    }
}

From source file:org.specrunner.converters.core.ConverterDateMidnightPatternArgs.java

License:Open Source License

@Override
public Object convert(Object value, Object[] args) throws ConverterException {
    if (value == null) {
        return null;
    }// w  w  w  .j av  a2s.c om
    if (value instanceof DateMidnight) {
        return value;
    }
    try {
        if (args.length < 1) {
            throw new ConverterException("Converter '" + getClass()
                    + "' missing pattern argument information (i.e. arg0=\"dd/MM/yyyy\").");
        }
        String pattern = String.valueOf(args[0]);
        synchronized (cache) {
            DateTimeFormatter formatter = cache.get(pattern);
            if (formatter == null) {
                formatter = DateTimeFormat.forPattern(pattern);
                cache.put(pattern, formatter);
            }
            TimeZone tz = getZone();
            if (tz != null) {
                formatter = formatter.withZone(DateTimeZone.forTimeZone(tz));
            }
            return formatter.parseDateTime(String.valueOf(value)).toDateMidnight();
        }
    } catch (IllegalArgumentException e) {
        throw new ConverterException(e);
    }
}

From source file:org.specrunner.converters.core.ConverterDateTimePatternArgs.java

License:Open Source License

@Override
public Object convert(Object value, Object[] args) throws ConverterException {
    if (value == null) {
        return null;
    }//from   ww w . ja  va 2 s .c om
    if (value instanceof DateTime) {
        return value;
    }
    try {
        if (args.length < 1) {
            throw new ConverterException("Converter '" + getClass()
                    + "' missing pattern argument information (i.e. arg0=\"dd/MM/yyyy\").");
        }
        String pattern = String.valueOf(args[0]);
        synchronized (cache) {
            DateTimeFormatter formatter = cache.get(pattern);
            if (formatter == null) {
                formatter = DateTimeFormat.forPattern(pattern);
                cache.put(pattern, formatter);
            }
            TimeZone tz = getZone();
            if (tz != null) {
                formatter = formatter.withZone(DateTimeZone.forTimeZone(tz));
            }
            return formatter.parseDateTime(String.valueOf(value));
        }
    } catch (IllegalArgumentException e) {
        throw new ConverterException(e);
    }
}

From source file:org.specrunner.converters.core.ConverterMutableDateTimePatternArgs.java

License:Open Source License

@Override
public Object convert(Object value, Object[] args) throws ConverterException {
    if (value == null) {
        return null;
    }//from  w w w  . j  av  a  2  s .  c  om
    if (value instanceof DateTime) {
        return value;
    }
    try {
        if (args.length < 1) {
            throw new ConverterException("Converter '" + getClass()
                    + "' missing pattern argument information (i.e. arg0=\"dd/MM/yyyy\").");
        }
        String pattern = String.valueOf(args[0]);
        synchronized (cache) {
            DateTimeFormatter formatter = cache.get(pattern);
            if (formatter == null) {
                formatter = DateTimeFormat.forPattern(pattern);
                cache.put(pattern, formatter);
            }
            TimeZone tz = getZone();
            if (tz != null) {
                formatter = formatter.withZone(DateTimeZone.forTimeZone(tz));
            }
            return formatter.parseDateTime(String.valueOf(value)).toMutableDateTime();
        }
    } catch (IllegalArgumentException e) {
        throw new ConverterException(e);
    }
}

From source file:org.springframework.format.datetime.joda.DateTimeFormatterFactory.java

License:Apache License

/**
 * Create a new {@code DateTimeFormatter} using this factory.
 * <p>If no specific pattern or style has been defined,
 * the supplied {@code fallbackFormatter} will be used.
 * @param fallbackFormatter the fall-back formatter to use when no specific
 * factory properties have been set (can be {@code null}).
 * @return a new date time formatter/* ww  w.  ja  v a  2s.  co  m*/
 */
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
    DateTimeFormatter dateTimeFormatter = null;
    if (StringUtils.hasLength(this.pattern)) {
        dateTimeFormatter = DateTimeFormat.forPattern(this.pattern);
    } else if (this.iso != null && this.iso != ISO.NONE) {
        switch (this.iso) {
        case DATE:
            dateTimeFormatter = ISODateTimeFormat.date();
            break;
        case TIME:
            dateTimeFormatter = ISODateTimeFormat.time();
            break;
        case DATE_TIME:
            dateTimeFormatter = ISODateTimeFormat.dateTime();
            break;
        case NONE:
            /* no-op */
            break;
        default:
            throw new IllegalStateException("Unsupported ISO format: " + this.iso);
        }
    } else if (StringUtils.hasLength(this.style)) {
        dateTimeFormatter = DateTimeFormat.forStyle(this.style);
    }

    if (dateTimeFormatter != null && this.timeZone != null) {
        dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
    }
    return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}

From source file:org.springframework.format.datetime.joda.JodaTimeContext.java

License:Apache License

/**
 * Get the DateTimeFormatter with the this context's settings
 * applied to the base {@code formatter}.
 * @param formatter the base formatter that establishes default
 * formatting rules, generally context-independent
 * @return the contextual DateTimeFormatter
 */// ww  w.j a  v  a2s .c  o  m
public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
    if (this.chronology != null) {
        formatter = formatter.withChronology(this.chronology);
    }
    if (this.timeZone != null) {
        formatter = formatter.withZone(this.timeZone);
    } else {
        LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
        if (localeContext instanceof TimeZoneAwareLocaleContext) {
            TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
            if (timeZone != null) {
                formatter = formatter.withZone(DateTimeZone.forTimeZone(timeZone));
            }
        }
    }
    return formatter;
}

From source file:org.wicketopia.joda.util.format.JodaFormatSupport.java

License:Apache License

public T convertToObject(String value, Locale locale) {
    if (Strings.isEmpty(value)) {
        return null;
    }/*from  w  w w.  ja  v  a2 s .c o  m*/

    DateTimeFormatter format = formatProvider.getFormatter();

    if (format == null) {
        throw new IllegalStateException("format must be not null");
    }
    format = format.withLocale(locale).withPivotYear(pivotYear);
    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        // instantiate now/ current time
        MutableDateTime dt = new MutableDateTime();
        if (zone != null) {
            // set time zone for client
            format = format.withZone(DateTimeZone.forTimeZone(zone));
            dt.setZone(DateTimeZone.forTimeZone(zone));
        }
        try {
            // parse date retaining the time of the submission
            int result = format.parseInto(dt, value, 0);
            if (result < 0) {
                throw new ConversionException(new ParseException("unable to parse date " + value, ~result));
            }
        } catch (RuntimeException e) {
            throw new ConversionException(e);
        }
        // apply the server time zone to the parsed value
        dt.setZone(getServerTimeZone());
        return translator.fromDateTime(dt.toDateTime());
    } else {
        try {
            DateTime date = format.parseDateTime(value);
            return date == null ? null : translator.fromDateTime(date);
        } catch (RuntimeException e) {
            throw new ConversionException(e);
        }
    }
}

From source file:org.wicketopia.joda.util.format.JodaFormatSupport.java

License:Apache License

@SuppressWarnings("unchecked")
public String convertToString(T object, Locale locale) {
    if (object == null) {
        return "";
    }//from  w w  w  . j  a  va 2 s .c om
    DateTime dt = translator.toDateTime((T) object);
    DateTimeFormatter format = formatProvider.getFormatter();
    format = format.withPivotYear(pivotYear).withLocale(locale);
    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        if (zone != null) {
            format = format.withZone(DateTimeZone.forTimeZone(zone));
        }
    }
    return format.print(dt);
}