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:stroom.util.date.DateUtil.java

License:Apache License

/**
 * Parse a date using a format.//w ww.j a v  a  2s .c  o  m
 *
 * @param pattern
 *            pattern to match
 * @param timeZoneId
 *            if provided the pattern will append Z and the time zone will
 *            be added
 * @param value
 *            value to parse
 * @return the result
 * @throws IllegalArgumentException
 *             if date does not parse
 */
public static long parseDate(final String pattern, final String timeZoneId, final String value) {
    DateTimeZone dateTimeZone = null;
    DateTime dateTime = null;

    if (value == null || value.trim().length() == 0) {
        throw new IllegalArgumentException("Unable to parse date: \"" + value + '"');
    }

    // Try to parse the time zone first.
    try {
        if (timeZoneId != null) {
            if (GMT_BST_GUESS.equals(timeZoneId)) {
                dateTimeZone = EUROPE_LONDON_TIME_ZONE;
            } else {
                dateTimeZone = DateTimeZone.forID(timeZoneId);
            }
        }
    } catch (final IllegalArgumentException e) {
        LOGGER.debug(e, e);
    }

    DateTimeFormatter dateFormat = buildDateFormatter(pattern);
    if (dateTimeZone != null) {
        try {
            dateFormat = dateFormat.withZone(dateTimeZone);
            dateTime = dateFormat.parseDateTime(value);

        } catch (final IllegalArgumentException e) {
            LOGGER.debug(e, e);

            // We failed to use the time zone so try UTC.
            dateFormat = dateFormat.withZone(DateTimeZone.UTC);
            dateTime = dateFormat.parseDateTime(value);
        }
    } else {
        dateTime = dateFormat.parseDateTime(value);
    }

    if (dateTime == null) {
        throw new IllegalArgumentException("Unable to parse date: \"" + value + '"');
    }

    return dateTime.getMillis();
}

From source file:stroom.util.date.DateUtil.java

License:Apache License

private static DateTimeFormatter buildDateFormatter(final String pattern) {
    final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(pattern);
    return dateTimeFormatter.withZone(DateTimeZone.UTC);
}

From source file:trendulo.web.date.DateConverter.java

License:Apache License

public static long dateStringToTimestamp(String date, DateTimeZone dateTimeZone) {
    long timestamp = 0;
    DateTimeFormatter formatter = null;

    // Month/*from  w  ww. j a  va 2s  .  c  om*/
    if (date.length() == 6) {
        formatter = DateTimeFormat.forPattern("yyyyMM");
    } else if (date.length() == 8) {
        formatter = DateTimeFormat.forPattern("yyyyMMdd");
    } else if (date.length() == 10) {
        formatter = DateTimeFormat.forPattern("yyyyMMddHH");
    }

    if (formatter != null) {
        timestamp = formatter.withZone(dateTimeZone).parseMillis(date);
    }

    return timestamp;
}

From source file:trendulo.web.date.DateConverter.java

License:Apache License

/**
 * Get the date string for the start of time period days days ago from timestamp
 * @param days the number of days in the past from timestamp
 * @param timestamp the time reference/*from   w  w w . j a  v a  2  s  .c o m*/
 * @return a dateString in the form of yyyyMMdd or yyyyMMddHH
 */
public static String getStartDateString(int days, long timestamp) {
    DateTimeFormatter formatter = getFormatterForDays(days);

    // Do the date arithmetic ( timestamp - days )
    //DateTime dateTime = new DateTime( timestamp, DateTimeZone.UTC );
    DateTime dateTime = new DateTime(timestamp, mdtDateTimeZone);
    DateTime startDateTime = dateTime.minusDays(days);

    //return formatter.withZoneUTC().print( startDateTime );
    return formatter.withZone(mdtDateTimeZone).print(startDateTime);
}

From source file:trendulo.web.date.DateConverter.java

License:Apache License

public static String getEndDateString(int days, long timestamp) {
    DateTimeFormatter formatter = getFormatterForDays(days);
    //DateTime dateTime = new DateTime( timestamp, DateTimeZone.UTC );
    DateTime dateTime = new DateTime(timestamp, mdtDateTimeZone);
    //return formatter.withZoneUTC().print( dateTime );
    return formatter.withZone(mdtDateTimeZone).print(dateTime);
}