blob: bbc776c24ee321e5391ee457ab912e9651a5b71f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package com.juick.www.formatter;
import com.juick.util.DateFormatter;
import org.springframework.context.MessageSource;
import org.springframework.format.Formatter;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Created by aalexeev on 11/22/16.
*/
public class SpringDateFormatter implements Formatter<Date> {
@Resource
private MessageSource messageSource;
private ConcurrentMap<Locale, com.juick.util.DateFormatter> formattersMap =
new ConcurrentHashMap<>(4, 0.75f, 2); // MAX 4 languages and 4/2=2 threads for write
@Override
public Date parse(final String text, final Locale locale) throws ParseException {
DateFormatter formatter = formattersMap.getOrDefault(
locale, createFormatter(locale));
return formatter.parse(text);
}
@Override
public String print(final Date object, final Locale locale) {
DateFormatter formatter = formattersMap.getOrDefault(
locale, createFormatter(locale));
return formatter.format(object);
}
private DateFormatter createFormatter(final Locale locale) {
String pattern = messageSource.getMessage("date.format", null, locale);
return new DateFormatter(pattern);
}
}
|