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.wicketstuff.calendar.util.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
 *      java.util.Locale)// ww  w . ja v a2 s . c o  m
 */
public Object convertToObject(String value, Locale locale) {
    DateTimeFormatter format = getFormat();

    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));
        }
        // parse date retaining the time of the submission
        format.parseInto(dt, value, 0);
        // apply the server time zone to the parsed value
        dt.setZone(getTimeZone());
        return dt.toDate();
    } else {
        return format.parseDateTime(value).toDate();
    }
}

From source file:org.wicketstuff.calendar.util.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object,
 *      java.util.Locale)//from   w w  w  .  ja va 2 s  . c o  m
 */
public String convertToString(Object value, Locale locale) {
    DateTime dt = new DateTime(((Date) value).getTime(), getTimeZone());
    DateTimeFormatter format = getFormat();

    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        if (zone != null) {
            // apply time zone to formatter
            format = format.withZone(DateTimeZone.forTimeZone(zone));
        }
    }
    return format.print(dt);
}

From source file:org.wicketstuff.datetime.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
 *      java.util.Locale)//from w w w. java2  s .c o m
 */
@Override
public Date convertToObject(String value, Locale locale) {
    if (Strings.isEmpty(value)) {
        return null;
    }

    DateTimeFormatter format = getFormat(locale);
    if (format == null) {
        throw new IllegalStateException("format must be not null");
    }

    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        DateTime dateTime;

        // set time zone for client
        format = format.withZone(getTimeZone());

        try {
            // parse date retaining the time of the submission
            dateTime = format.parseDateTime(value);
        } catch (RuntimeException e) {
            throw newConversionException(e, locale);
        }
        // apply the server time zone to the parsed value
        if (zone != null) {
            dateTime = dateTime.withZoneRetainFields(DateTimeZone.forTimeZone(zone));
        }

        return dateTime.toDate();
    } else {
        try {
            DateTime date = format.parseDateTime(value);
            return date.toDate();
        } catch (RuntimeException e) {
            throw newConversionException(e, locale);
        }
    }
}

From source file:org.wicketstuff.datetime.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object,
 *      java.util.Locale)//from   www  .  jav  a2  s .  c o m
 */
@Override
public String convertToString(Date value, Locale locale) {
    DateTime dt = new DateTime(value.getTime(), getTimeZone());
    DateTimeFormatter format = getFormat(locale);

    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        if (zone != null) {
            // apply time zone to formatter
            format = format.withZone(DateTimeZone.forTimeZone(zone));
        }
    }
    return format.print(dt);
}

From source file:org.xmlcml.euclid.JodaDate.java

License:Apache License

public static DateTime parseDate(String date, String format) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(format);
    if (format.endsWith("Z")) {
    } else {//  w  ww .  j a va 2 s.  co  m
        dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forID("UTC"));
    }
    DateTime dateTime = dateTimeFormatter.parseDateTime(date);
    return dateTime.withZone(DateTimeZone.forID("UTC"));
}

From source file:se.vgregion.pubsub.content.DateTimeUtils.java

License:Open Source License

/**
 * Parses common date time formats in feeds
 * @param value/*from   w  w  w  .j  a va2s.  c o  m*/
 * @return
 */
public static DateTime parseDateTime(String value) {
    // Tue, 18 Jan 2011 07:42:14 +0000

    DateTimeFormatter parser;
    if (Character.isDigit(value.charAt(0))) {
        // assume ISO
        parser = ISODateTimeFormat.dateTimeParser();
    } else {
        // assume RSS datetime
        parser = new DateTimeFormatterBuilder().appendDayOfWeekShortText().appendLiteral(", ")
                .appendDayOfMonth(1).appendLiteral(" ").appendMonthOfYearShortText().appendLiteral(" ")
                .appendYear(4, 4).appendLiteral(" ").appendHourOfDay(2).appendLiteral(":").appendMinuteOfHour(2)
                .appendLiteral(":").appendSecondOfMinute(2).appendLiteral(" +0000").toFormatter();
    }

    parser = parser.withZone(DateTimeZone.UTC).withLocale(Locale.US);
    return parser.parseDateTime(value);
}

From source file:stirling.fix.messages.MonthYearField.java

License:Apache License

@Override
public void parse(String value) {
    DateTimeFormatter fmt = getFormat();
    try {/*ww  w  . ja  v a  2 s.  com*/
        this.value = fmt.withZone(UTC).parseDateTime(value);
    } catch (Exception e) {
        validFormat = false;
    }
}

From source file:stirling.fix.messages.MonthYearField.java

License:Apache License

@Override
protected String value() {
    if (!hasValue()) {
        return null;
    }// ww  w.j a  va  2  s.co m
    DateTimeFormatter fmt = getFormat();
    return fmt.withZone(UTC).print(value);
}

From source file:stirling.fix.messages.UtcTimestampField.java

License:Apache License

@Override
public void parse(String value) {
    if (value.length() == FORMAT_WITH_MSEC.length())
        format = FORMAT_WITH_MSEC;/*  ww w  .ja  va2s  .  c  o  m*/
    else
        format = FORMAT_WITHOUT_MSEC;
    DateTimeFormatter fmt = getFormat();
    try {
        this.value = fmt.withZone(UTC).parseDateTime(value);
    } catch (Exception e) {
        validFormat = false;
    }
}

From source file:stroom.pipeline.server.xsltfunctions.FormatDate.java

License:Apache License

private Sequence convertToSpecifiedDateFormat(final String functionName, final XPathContext context,
        final Sequence[] arguments) throws XPathException {
    Sequence result = StringValue.EMPTY_STRING;
    final String date = getSafeString(functionName, context, arguments, 0);
    final String patternIn = getSafeString(functionName, context, arguments, 1);
    final String timeZoneIn = getSafeString(functionName, context, arguments, 2);
    final String patternOut = getSafeString(functionName, context, arguments, 3);
    String timeZoneOut = null;/*from ww w. j  a va2 s  .  co  m*/
    if (arguments.length == 5) {
        timeZoneOut = getSafeString(functionName, context, arguments, 4);
    }

    // Parse the supplied date.
    long ms = -1;
    try {
        ms = DateUtil.parseDate(patternIn, timeZoneIn, date);
    } catch (final Throwable e) {
        final StringBuilder sb = new StringBuilder();
        sb.append("Failed to parse date: \"");
        sb.append(date);
        sb.append("\" (Pattern: ");
        sb.append(patternIn);
        sb.append(", Time Zone: ");
        sb.append(timeZoneIn);
        sb.append(")");
        outputWarning(context, sb, e);
    }

    if (ms != -1) {
        // Resolve the output time zone.
        final DateTimeZone dateTimeZone = getTimeZone(context, timeZoneOut);
        if (dateTimeZone != null) {
            try {
                // Now format the date using the specified pattern and time
                // zone.
                DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(patternOut);
                dateTimeFormatter = dateTimeFormatter.withZone(dateTimeZone);
                final String time = dateTimeFormatter.print(ms);
                result = StringValue.makeStringValue(time);
            } catch (final Throwable e) {
                final StringBuilder sb = new StringBuilder();
                sb.append("Failed to format date: \"");
                sb.append(date);
                sb.append("\" (Pattern: ");
                sb.append(patternOut);
                sb.append(", Time Zone: ");
                sb.append(timeZoneOut);
                sb.append(")");
                outputWarning(context, sb, e);
            }
        }
    }

    return result;
}