commit
52295fff0f
@ -0,0 +1,63 @@
|
||||
# Spring DateTimeFormatAnnotationFormatterFactory
|
||||
|
||||
- 类全路径: `org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory`
|
||||
|
||||
- 类图
|
||||
![EmbeddedValueResolutionSupport](/images/spring/DateTimeFormatAnnotationFormatterFactory.png)
|
||||
|
||||
|
||||
```java
|
||||
public class DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
|
||||
implements AnnotationFormatterFactory<DateTimeFormat> {
|
||||
|
||||
/**
|
||||
* 字段类型
|
||||
*/
|
||||
private static final Set<Class<?>> FIELD_TYPES;
|
||||
|
||||
@Override
|
||||
public Set<Class<?>> getFieldTypes() {
|
||||
return FIELD_TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
|
||||
return getFormatter(annotation, fieldType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
|
||||
return getFormatter(annotation, fieldType);
|
||||
}
|
||||
|
||||
protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
|
||||
DateFormatter formatter = new DateFormatter();
|
||||
// style
|
||||
String style = resolveEmbeddedValue(annotation.style());
|
||||
// 判断时间格式是否村子啊
|
||||
if (StringUtils.hasLength(style)) {
|
||||
formatter.setStylePattern(style);
|
||||
}
|
||||
// iso 设置
|
||||
formatter.setIso(annotation.iso());
|
||||
// date time pattern
|
||||
String pattern = resolveEmbeddedValue(annotation.pattern());
|
||||
// 设置
|
||||
if (StringUtils.hasLength(pattern)) {
|
||||
formatter.setPattern(pattern);
|
||||
}
|
||||
return formatter;
|
||||
}
|
||||
|
||||
static {
|
||||
Set<Class<?>> fieldTypes = new HashSet<>(4);
|
||||
// 加入字段类型
|
||||
fieldTypes.add(Date.class);
|
||||
fieldTypes.add(Calendar.class);
|
||||
fieldTypes.add(Long.class);
|
||||
FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
@ -0,0 +1,29 @@
|
||||
# Spring DateTimeParser
|
||||
- 类全路径: `org.springframework.format.datetime.joda.DateTimeParser`
|
||||
|
||||
- 代码如下
|
||||
|
||||
```java
|
||||
public final class DateTimeParser implements Parser<DateTime> {
|
||||
|
||||
private final DateTimeFormatter formatter;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new DateTimeParser.
|
||||
* @param formatter the Joda DateTimeFormatter instance
|
||||
*/
|
||||
public DateTimeParser(DateTimeFormatter formatter) {
|
||||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public DateTime parse(String text, Locale locale) throws ParseException {
|
||||
// DateTimeFormatter 转换字符串事件类型
|
||||
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
@ -0,0 +1,28 @@
|
||||
# Spring MillisecondInstantPrinter
|
||||
- 类全路径: `org.springframework.format.datetime.joda.MillisecondInstantPrinter`
|
||||
|
||||
|
||||
```java
|
||||
public final class MillisecondInstantPrinter implements Printer<Long> {
|
||||
|
||||
private final DateTimeFormatter formatter;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new ReadableInstantPrinter.
|
||||
* @param formatter the Joda DateTimeFormatter instance
|
||||
*/
|
||||
public MillisecondInstantPrinter(DateTimeFormatter formatter) {
|
||||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String print(Long instant, Locale locale) {
|
||||
// DateTimeFormatter .print
|
||||
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
```
|
@ -0,0 +1,42 @@
|
||||
# Spring AnnotationFormatterFactory
|
||||
|
||||
- 类全路径: `org.springframework.format.AnnotationFormatterFactory`
|
||||
|
||||
|
||||
```java
|
||||
|
||||
public interface AnnotationFormatterFactory<A extends Annotation> {
|
||||
|
||||
/**
|
||||
* The types of fields that may be annotated with the <A> annotation.
|
||||
* 字段类型
|
||||
*/
|
||||
Set<Class<?>> getFieldTypes();
|
||||
|
||||
/**
|
||||
* Get the Printer to print the value of a field of {@code fieldType} annotated with
|
||||
* {@code annotation}.
|
||||
* <p>If the type T the printer accepts is not assignable to {@code fieldType}, a
|
||||
* coercion from {@code fieldType} to T will be attempted before the Printer is invoked.
|
||||
* 通过注解和字段类型获取输出接口
|
||||
* @param annotation the annotation instance
|
||||
* @param fieldType the type of field that was annotated
|
||||
* @return the printer
|
||||
*/
|
||||
Printer<?> getPrinter(A annotation, Class<?> fieldType);
|
||||
|
||||
/**
|
||||
* Get the Parser to parse a submitted value for a field of {@code fieldType}
|
||||
* annotated with {@code annotation}.
|
||||
* <p>If the object the parser returns is not assignable to {@code fieldType},
|
||||
* a coercion to {@code fieldType} will be attempted before the field is set.
|
||||
* 通过注解和字段类型获取解析接口
|
||||
* @param annotation the annotation instance
|
||||
* @param fieldType the type of field that was annotated
|
||||
* @return the parser
|
||||
*/
|
||||
Parser<?> getParser(A annotation, Class<?> fieldType);
|
||||
|
||||
}
|
||||
|
||||
```
|
@ -0,0 +1,11 @@
|
||||
# Spring Formatter
|
||||
- 类全路径: `org.springframework.format.Formatter`
|
||||
|
||||
```java
|
||||
public interface Formatter<T> extends Printer<T>, Parser<T> {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
- 该接口继承了 printer 和 parser 两个接口.
|
||||
- 比较常见的有: `DateFormatter` 就是继承这个接口.
|
@ -0,0 +1,27 @@
|
||||
# Spring Parser
|
||||
|
||||
- 类全路径: `org.springframework.format.Parser`
|
||||
- 类作用: 字符串准换成java对象
|
||||
|
||||
```java
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Parser<T> {
|
||||
|
||||
/**
|
||||
* Parse a text String to produce a T.
|
||||
* 将字符串转换成对象
|
||||
* @param text the text string
|
||||
* @param locale the current user locale
|
||||
* @return an instance of T
|
||||
* @throws ParseException when a parse exception occurs in a java.text parsing library
|
||||
* @throws IllegalArgumentException when a parse exception occurs
|
||||
*/
|
||||
T parse(String text, Locale locale) throws ParseException;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
- 类图
|
||||
|
||||
![Parser](/images/spring/Parser.png)
|
@ -0,0 +1,20 @@
|
||||
# Spring Printer
|
||||
- 类全路径: `org.springframework.format.Printer`
|
||||
- 类作用: 对象转换成字符串
|
||||
|
||||
|
||||
```java
|
||||
@FunctionalInterface
|
||||
public interface Printer<T> {
|
||||
|
||||
/**
|
||||
* Print the object of type T for display.
|
||||
* 打印对象
|
||||
* @param object the instance to print
|
||||
* @param locale the current user locale
|
||||
* @return the printed text string
|
||||
*/
|
||||
String print(T object, Locale locale);
|
||||
|
||||
}
|
||||
```
|
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 41 KiB |
Loading…
Reference in new issue