001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.format.datetime.joda; 018 019import java.util.Locale; 020 021import org.joda.time.format.DateTimeFormatter; 022 023import org.springframework.core.NamedThreadLocal; 024import org.springframework.lang.Nullable; 025 026/** 027 * A holder for a thread-local {@link JodaTimeContext} 028 * with user-specific Joda-Time settings. 029 * 030 * @author Keith Donald 031 * @author Juergen Hoeller 032 * @since 3.0 033 * @see org.springframework.context.i18n.LocaleContextHolder 034 */ 035public final class JodaTimeContextHolder { 036 037 private static final ThreadLocal<JodaTimeContext> jodaTimeContextHolder = 038 new NamedThreadLocal<>("JodaTimeContext"); 039 040 041 private JodaTimeContextHolder() { 042 } 043 044 045 /** 046 * Reset the JodaTimeContext for the current thread. 047 */ 048 public static void resetJodaTimeContext() { 049 jodaTimeContextHolder.remove(); 050 } 051 052 /** 053 * Associate the given JodaTimeContext with the current thread. 054 * @param jodaTimeContext the current JodaTimeContext, 055 * or {@code null} to reset the thread-bound context 056 */ 057 public static void setJodaTimeContext(@Nullable JodaTimeContext jodaTimeContext) { 058 if (jodaTimeContext == null) { 059 resetJodaTimeContext(); 060 } 061 else { 062 jodaTimeContextHolder.set(jodaTimeContext); 063 } 064 } 065 066 /** 067 * Return the JodaTimeContext associated with the current thread, if any. 068 * @return the current JodaTimeContext, or {@code null} if none 069 */ 070 @Nullable 071 public static JodaTimeContext getJodaTimeContext() { 072 return jodaTimeContextHolder.get(); 073 } 074 075 076 /** 077 * Obtain a DateTimeFormatter with user-specific settings applied to the given base Formatter. 078 * @param formatter the base formatter that establishes default formatting rules 079 * (generally user independent) 080 * @param locale the current user locale (may be {@code null} if not known) 081 * @return the user-specific DateTimeFormatter 082 */ 083 public static DateTimeFormatter getFormatter(DateTimeFormatter formatter, @Nullable Locale locale) { 084 DateTimeFormatter formatterToUse = (locale != null ? formatter.withLocale(locale) : formatter); 085 JodaTimeContext context = getJodaTimeContext(); 086 return (context != null ? context.getFormatter(formatterToUse) : formatterToUse); 087 } 088 089}