spring DateTimeContext 源码
spring DateTimeContext 代码
文件路径:/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeContext.java
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.format.datetime.standard;
import java.time.ZoneId;
import java.time.chrono.Chronology;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.i18n.TimeZoneAwareLocaleContext;
import org.springframework.lang.Nullable;
/**
* A context that holds user-specific <code>java.time</code> (JSR-310) settings
* such as the user's Chronology (calendar system) and time zone.
* <p>A {@code null} property value indicates the user has not specified a setting.
*
* @author Juergen Hoeller
* @since 4.0
* @see DateTimeContextHolder
*/
public class DateTimeContext {
@Nullable
private Chronology chronology;
@Nullable
private ZoneId timeZone;
/**
* Set the user's chronology (calendar system).
*/
public void setChronology(@Nullable Chronology chronology) {
this.chronology = chronology;
}
/**
* Return the user's chronology (calendar system), if any.
*/
@Nullable
public Chronology getChronology() {
return this.chronology;
}
/**
* Set the user's time zone.
* <p>Alternatively, set a {@link TimeZoneAwareLocaleContext} on
* {@link LocaleContextHolder}. This context class will fall back to
* checking the locale context if no setting has been provided here.
* @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
* @see org.springframework.context.i18n.LocaleContextHolder#setLocaleContext
*/
public void setTimeZone(@Nullable ZoneId timeZone) {
this.timeZone = timeZone;
}
/**
* Return the user's time zone, if any.
*/
@Nullable
public ZoneId getTimeZone() {
return this.timeZone;
}
/**
* Get the DateTimeFormatter with 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
*/
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(timeZone.toZoneId());
}
}
}
return formatter;
}
}
相关信息
相关文章
spring DateTimeContextHolder 源码
spring DateTimeFormatterFactory 源码
spring DateTimeFormatterFactoryBean 源码
spring DateTimeFormatterRegistrar 源码
spring DateTimeFormatterUtils 源码
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦