Java Code Examples for org.joda.time.format.DateTimeFormatter#withZone()

The following examples show how to use org.joda.time.format.DateTimeFormatter#withZone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeUtil.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public String getIsoDateWithLocalTime(Date dateToConvert) {
    if (dateToConvert == null) {
        return null;
    }
    DateTime dt = new DateTime(dateToConvert);
    DateTimeFormatter fmt = ISODateTimeFormat.yearMonthDay();
    DateTimeFormatter localFmt = fmt.withLocale(new ResourceLoader().getLocale());
    DateTimeFormatter fmtTime = DateTimeFormat.shortTime();
    DateTimeFormatter localFmtTime = fmtTime.withLocale(new ResourceLoader().getLocale());

    // If the client browser is in a different timezone than server, need to modify date
    if (m_client_timezone !=null && m_server_timezone!=null && !m_client_timezone.hasSameRules(m_server_timezone)) {
      DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(m_client_timezone);
      localFmt = localFmt.withZone(dateTimeZone);
      localFmtTime = localFmtTime.withZone(dateTimeZone);
    }
    return dt.toString(localFmt) + " " + dt.toString(localFmtTime);
}
 
Example 2
Source File: TimeUtil.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public String getDateTimeWithTimezoneConversion(Date dateToConvert) {
    if (dateToConvert == null) {
        return null;
    }
    DateTime dt = new DateTime(dateToConvert);
    DateTimeFormatter fmt = ISODateTimeFormat.yearMonthDay();
    DateTimeFormatter fmtTime = ISODateTimeFormat.hourMinuteSecond();

    // If the client browser is in a different timezone than server, need to modify date
    if (m_client_timezone !=null && m_server_timezone!=null && !m_client_timezone.hasSameRules(m_server_timezone)) {
      DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(m_client_timezone);
      fmt = fmt.withZone(dateTimeZone);
      fmtTime = fmtTime.withZone(dateTimeZone);
    }
    return dt.toString(fmt) + " " + dt.toString(fmtTime);
}
 
Example 3
Source File: AbstractMultiFormatContextualConverterFactory.java    From SimpleFlatMapper with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public ContextualConverter<? super I, ? extends O> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {

    DateTimeFormatter[] dateTimeFormatters = JodaTimeHelper.getDateTimeFormatters(params);

    DateTimeZone zoneId = JodaTimeHelper.getDateTimeZoneOrDefault(params);

    ContextualConverter<I, O>[] converters = new ContextualConverter[dateTimeFormatters.length];

    for(int i = 0; i < dateTimeFormatters.length; i++) {
        DateTimeFormatter dateTimeFormatter = dateTimeFormatters[i];
        if (dateTimeFormatter.getZone() == null) {
            dateTimeFormatter.withZone(zoneId);
        }

        converters[i] = newConverter(dateTimeFormatter);
    }

    if (converters.length == 1) {
        return converters[0];
    } else {
        return new MultiDateTimeFormatterConverter<I, O>(converters);
    }

}
 
Example 4
Source File: TimeUtil.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public String getIsoDateWithLocalTime(Date dateToConvert) {
    if (dateToConvert == null) {
        return null;
    }
    DateTime dt = new DateTime(dateToConvert);
    DateTimeFormatter fmt = ISODateTimeFormat.yearMonthDay();
    DateTimeFormatter localFmt = fmt.withLocale(new ResourceLoader().getLocale());
    DateTimeFormatter fmtTime = DateTimeFormat.shortTime();
    DateTimeFormatter localFmtTime = fmtTime.withLocale(new ResourceLoader().getLocale());

    // If the client browser is in a different timezone than server, need to modify date
    if (m_client_timezone !=null && m_server_timezone!=null && !m_client_timezone.hasSameRules(m_server_timezone)) {
      DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(m_client_timezone);
      localFmt = localFmt.withZone(dateTimeZone);
      localFmtTime = localFmtTime.withZone(dateTimeZone);
    }
    return dt.toString(localFmt) + " " + dt.toString(localFmtTime);
}
 
Example 5
Source File: TimeUtil.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public String getDateTimeWithTimezoneConversion(Date dateToConvert) {
    if (dateToConvert == null) {
        return null;
    }
    DateTime dt = new DateTime(dateToConvert);
    DateTimeFormatter fmt = ISODateTimeFormat.yearMonthDay();
    DateTimeFormatter fmtTime = ISODateTimeFormat.hourMinuteSecond();

    // If the client browser is in a different timezone than server, need to modify date
    if (m_client_timezone !=null && m_server_timezone!=null && !m_client_timezone.hasSameRules(m_server_timezone)) {
      DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(m_client_timezone);
      fmt = fmt.withZone(dateTimeZone);
      fmtTime = fmtTime.withZone(dateTimeZone);
    }
    return dt.toString(fmt) + " " + dt.toString(fmtTime);
}
 
Example 6
Source File: DateTimeService.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
private static DateTimeFormatter getDateTimeFormatter(final String localeLang, final String localeCountry, final String timezone, final String dateFormat) {
    DateTimeFormatter formatter;
    if (StringUtilities.isNoneBlank(dateFormat)) {
        formatter = DateTimeFormat.forPattern(dateFormat);
    } else {
        formatter = DateTimeFormat.longDateTime();
    }

    if (isNotBlank(timezone)) {
        formatter = formatter.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezone)));
    }

    if (isNotBlank(localeLang)) {
        formatter = formatter.withLocale(DateTimeUtils.getLocaleByCountry(localeLang, localeCountry));
    }
    return formatter;
}
 
Example 7
Source File: DateTimeFormatterFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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
 * @return a new date time formatter
 */
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;
			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);
}
 
Example 8
Source File: DateTimeFormatterFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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
 * @return a new date time formatter
 */
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;
			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);
}
 
Example 9
Source File: DateTimeFormatterFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 */
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);
}
 
Example 10
Source File: DateMathParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private long parseDateTime(String value, DateTimeZone timeZone) {
    DateTimeFormatter parser = dateTimeFormatter.parser();
    if (timeZone != null) {
        parser = parser.withZone(timeZone);
    }
    try {
        return parser.parseMillis(value);
    } catch (IllegalArgumentException e) {
        
        throw new ElasticsearchParseException("failed to parse date field [{}] with format [{}]", e, value, dateTimeFormatter.format());
    }
}
 
Example 11
Source File: DateTimeFormatterFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 */
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);
}
 
Example 12
Source File: JodaTimeHelper.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
private static DateTimeFormatter withZone(DateTimeFormatter dateTimeFormatter, DateTimeZone zoneId) {
    if (zoneId != null) {
        return dateTimeFormatter.withZone(zoneId);
    } else if (dateTimeFormatter.getZone() == null) {
        return dateTimeFormatter.withZone(DateTimeZone.getDefault());
    }
    return dateTimeFormatter;
}
 
Example 13
Source File: JodaDateFormatter.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
public JodaDateFormatter(String pattern, Locale locale, DateTimeZone timeZone) {
	if (pattern == null) {
		pattern = Defaults.DEFAULT_DATE_FORMAT;
	}
	if (locale == null) {
		locale = MiscUtils.getDefaultLocale();
	}
	DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern).withLocale(locale);
	if (timeZone != null) {
		formatter = formatter.withZone(timeZone);
	}
	this.dateTimeFormatter = formatter;
	this.pattern = pattern;
	this.locale = locale;
}
 
Example 14
Source File: DateTimeConverter.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
private DateTimeFormatter getDateFormat(UIComponent uiComponent) {
	DateTimeFormatter format = DateTimeFormat.forPattern(getPattern(uiComponent));

	format = format.withLocale(SessionPreferences.getCurrentLocale());
	format = format.withZone(getTimeZone());

	return format;
}
 
Example 15
Source File: DateTimeService.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private static DateTime parseInputDate(final String date, final String dateFormat, final String dateLocaleLang, final String dateLocaleCountry,
                                       final DateTimeZone timeZone) throws Exception {
    if (DateTimeUtils.isUnix(dateFormat)) {
        return new DateTime(Long.parseLong(date) * Constants.Miscellaneous.THOUSAND_MULTIPLIER).withZone(timeZone);
    }
    if (DateTimeUtils.isMilliseconds(dateFormat)) { // can be removed if not needed
        return new DateTime(new Date(Long.parseLong(date))).withZone(timeZone);
    }
    DateTimeFormatter dateFormatter = DateTimeUtils.getDateFormatter(dateFormat, dateLocaleLang, dateLocaleCountry);
    if (isNotBlank(dateFormat)) {
        dateFormatter = dateFormatter.withZone(timeZone);
        return dateFormatter.parseDateTime(date);
    }
    return DateTimeUtils.getJodaOrJavaDate(dateFormatter, date);
}