下面列出了org.joda.time.format.DateTimeFormatter#withZone ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
@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);
}
}
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);
}
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);
}
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;
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
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());
}
}
/**
* 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);
}
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;
}
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;
}
private DateTimeFormatter getDateFormat(UIComponent uiComponent) {
DateTimeFormatter format = DateTimeFormat.forPattern(getPattern(uiComponent));
format = format.withLocale(SessionPreferences.getCurrentLocale());
format = format.withZone(getTimeZone());
return format;
}
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);
}