polishing
This commit is contained in:
parent
1d005e12af
commit
5fdc29f152
|
|
@ -73,8 +73,6 @@ public @interface DateTimeFormat {
|
|||
|
||||
/**
|
||||
* Common ISO date time format patterns.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
public enum ISO {
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
|
@ -20,10 +21,12 @@ import java.util.Locale;
|
|||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import org.springframework.format.Parser;
|
||||
|
||||
/**
|
||||
* Parses Joda Time {@link DateTime} instances using a {@link DateTimeFormatter}.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
|
|
@ -32,7 +35,7 @@ public final class DateTimeParser implements Parser<DateTime> {
|
|||
private final DateTimeFormatter formatter;
|
||||
|
||||
/**
|
||||
* Creates a new DateTimeParser.
|
||||
* Create a new DateTimeParser.
|
||||
* @param formatter the Joda DateTimeFormatter instance
|
||||
*/
|
||||
public DateTimeParser(DateTimeFormatter formatter) {
|
||||
|
|
@ -42,4 +45,5 @@ public final class DateTimeParser implements Parser<DateTime> {
|
|||
public DateTime parse(String text, Locale locale) throws ParseException {
|
||||
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(text);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import org.joda.time.Chronology;
|
||||
|
|
@ -22,6 +23,7 @@ import org.joda.time.format.DateTimeFormatter;
|
|||
/**
|
||||
* A context that holds user-specific Joda Time settings such as the user's Chronology (calendar system) and time zone.
|
||||
* A <code>null</code> property value indicate the user has not specified a setting.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
* @see JodaTimeContextHolder
|
||||
|
|
@ -32,19 +34,27 @@ public class JodaTimeContext {
|
|||
|
||||
private DateTimeZone timeZone;
|
||||
|
||||
|
||||
/**
|
||||
* Set the user's chronology.
|
||||
*/
|
||||
public void setChronology(Chronology chronology) {
|
||||
this.chronology = chronology;
|
||||
}
|
||||
|
||||
/**
|
||||
* The user's chronology (calendar system).
|
||||
* Null if not specified.
|
||||
*/
|
||||
public Chronology getChronology() {
|
||||
return chronology;
|
||||
return this.chronology;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the user's chronology.
|
||||
* Set the user's timezone.
|
||||
*/
|
||||
public void setChronology(Chronology chronology) {
|
||||
this.chronology = chronology;
|
||||
public void setTimeZone(DateTimeZone timeZone) {
|
||||
this.timeZone = timeZone;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -55,12 +65,6 @@ public class JodaTimeContext {
|
|||
return timeZone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the user's timezone.
|
||||
*/
|
||||
public void setTimeZone(DateTimeZone timeZone) {
|
||||
this.timeZone = timeZone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Formatter with the this context's settings applied to the base <code>formatter</code>.
|
||||
|
|
|
|||
|
|
@ -13,30 +13,26 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import org.springframework.core.NamedInheritableThreadLocal;
|
||||
|
||||
/**
|
||||
* A holder for a thread-local user {@link JodaTimeContext}.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public final class JodaTimeContextHolder {
|
||||
|
||||
private static final ThreadLocal<JodaTimeContext> jodaTimeContextHolder = new NamedInheritableThreadLocal<JodaTimeContext>(
|
||||
"Joda Time Context");
|
||||
private static final ThreadLocal<JodaTimeContext> jodaTimeContextHolder =
|
||||
new NamedInheritableThreadLocal<JodaTimeContext>("JodaTime Context");
|
||||
|
||||
/**
|
||||
* Return the JodaTimeContext associated with the current thread, if any.
|
||||
* @return the current JodaTimeContext, or <code>null</code> if none
|
||||
*/
|
||||
public static JodaTimeContext getJodaTimeContext() {
|
||||
return jodaTimeContextHolder.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate the given JodaTimeContext with the current thread.
|
||||
|
|
@ -46,7 +42,15 @@ public final class JodaTimeContextHolder {
|
|||
public static void setJodaTimeContext(JodaTimeContext context) {
|
||||
jodaTimeContextHolder.set(context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the JodaTimeContext associated with the current thread, if any.
|
||||
* @return the current JodaTimeContext, or <code>null</code> if none
|
||||
*/
|
||||
public static JodaTimeContext getJodaTimeContext() {
|
||||
return jodaTimeContextHolder.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Formatter with the user-specific settings applied to thefrom the base <code>formatter</code>.
|
||||
* @param formatter the base formatter that establishes default formatting rules, generally user independent
|
||||
|
|
@ -58,7 +62,7 @@ public final class JodaTimeContextHolder {
|
|||
formatter = formatter.withLocale(locale);
|
||||
}
|
||||
JodaTimeContext context = getJodaTimeContext();
|
||||
return context != null ? context.getFormatter(formatter) : formatter;
|
||||
return (context != null ? context.getFormatter(formatter) : formatter);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
|
@ -24,22 +25,20 @@ import org.joda.time.LocalDate;
|
|||
import org.joda.time.LocalDateTime;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.joda.time.ReadableInstant;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||
|
||||
/**
|
||||
* Installs lower-level type converters required to integrate Joda Time support into Spring's field formatting system.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
final class JodaTimeConverters {
|
||||
|
||||
private JodaTimeConverters() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs the converters into the converter registry.
|
||||
* Install the converters into the converter registry.
|
||||
* @param registry the converter registry
|
||||
*/
|
||||
public static void registerConverters(ConverterRegistry registry) {
|
||||
|
|
@ -54,9 +53,8 @@ final class JodaTimeConverters {
|
|||
registry.addConverter(new CalendarToReadableInstantConverter());
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Used when binding a parsed DateTime to a LocalDate field.
|
||||
* @see DateTimeParser
|
||||
**/
|
||||
|
|
@ -129,7 +127,7 @@ final class JodaTimeConverters {
|
|||
/**
|
||||
* Used when printing a java.util.Date field with a MillisecondInstantPrinter.
|
||||
* @see MillisecondInstantPrinter
|
||||
* @see DateTimeFormatAnnotationFormatterFactory
|
||||
* @see JodaDateTimeFormatAnnotationFormatterFactory
|
||||
*/
|
||||
private static class DateToLongConverter implements Converter<Date, Long> {
|
||||
public Long convert(Date source) {
|
||||
|
|
@ -140,7 +138,7 @@ final class JodaTimeConverters {
|
|||
/**
|
||||
* Used when printing a java.util.Calendar field with a ReadableInstantPrinter.
|
||||
* @see MillisecondInstantPrinter
|
||||
* @see DateTimeFormatAnnotationFormatterFactory
|
||||
* @see JodaDateTimeFormatAnnotationFormatterFactory
|
||||
*/
|
||||
private static class CalendarToReadableInstantConverter implements Converter<Calendar, ReadableInstant> {
|
||||
public ReadableInstant convert(Calendar source) {
|
||||
|
|
|
|||
|
|
@ -13,15 +13,18 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import org.springframework.format.Printer;
|
||||
|
||||
/**
|
||||
* Prints Long instances using a {@link DateTimeFormatter}.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
|
|
@ -30,7 +33,7 @@ public final class MillisecondInstantPrinter implements Printer<Long> {
|
|||
private final DateTimeFormatter formatter;
|
||||
|
||||
/**
|
||||
* Creates a new ReadableInstantPrinter.
|
||||
* Create a new ReadableInstantPrinter.
|
||||
* @param formatter the Joda DateTimeFormatter instance
|
||||
*/
|
||||
public MillisecondInstantPrinter(DateTimeFormatter formatter) {
|
||||
|
|
@ -40,4 +43,5 @@ public final class MillisecondInstantPrinter implements Printer<Long> {
|
|||
public String print(Long instant, Locale locale) {
|
||||
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import java.util.Locale;
|
||||
|
|
@ -22,7 +23,8 @@ import org.joda.time.format.DateTimeFormatter;
|
|||
import org.springframework.format.Printer;
|
||||
|
||||
/**
|
||||
* Prints Joda Time {@link ReadableInstant} instances using a {@link DateTimeFormatter}.
|
||||
* Prints JodaTime {@link ReadableInstant} instances using a {@link DateTimeFormatter}.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
|
|
@ -31,7 +33,7 @@ public final class ReadableInstantPrinter implements Printer<ReadableInstant> {
|
|||
private final DateTimeFormatter formatter;
|
||||
|
||||
/**
|
||||
* Creates a new ReadableInstantPrinter.
|
||||
* Create a new ReadableInstantPrinter.
|
||||
* @param formatter the Joda DateTimeFormatter instance
|
||||
*/
|
||||
public ReadableInstantPrinter(DateTimeFormatter formatter) {
|
||||
|
|
@ -41,4 +43,5 @@ public final class ReadableInstantPrinter implements Printer<ReadableInstant> {
|
|||
public String print(ReadableInstant instant, Locale locale) {
|
||||
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,16 +13,19 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.joda.time.ReadablePartial;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import org.springframework.format.Printer;
|
||||
|
||||
/**
|
||||
* Prints Joda Time {@link ReadablePartial} instances using a {@link DateTimeFormatter}.
|
||||
* Prints JodaTime {@link ReadablePartial} instances using a {@link DateTimeFormatter}.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
|
|
@ -31,7 +34,7 @@ public final class ReadablePartialPrinter implements Printer<ReadablePartial> {
|
|||
private final DateTimeFormatter formatter;
|
||||
|
||||
/**
|
||||
* Creates a new ReadableInstantPrinter.
|
||||
* Create a new ReadableInstantPrinter.
|
||||
* @param formatter the Joda DateTimeFormatter instance
|
||||
*/
|
||||
public ReadablePartialPrinter(DateTimeFormatter formatter) {
|
||||
|
|
@ -41,4 +44,5 @@ public final class ReadablePartialPrinter implements Printer<ReadablePartial> {
|
|||
public String print(ReadablePartial partial, Locale locale) {
|
||||
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(partial);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue