From e7fd58f1023ce9e1c9ab880bf27c5c0b2a3cffca Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Wed, 23 Nov 2016 16:19:26 +0700 Subject: thymeleaf layout plugin used --- .../com/juick/www/formatter/DateFormatter.java | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java (limited to 'juick-spring-www/src/main/java/com/juick/www/formatter') diff --git a/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java b/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java new file mode 100644 index 00000000..74596fc5 --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java @@ -0,0 +1,43 @@ +package com.juick.www.formatter; + +import org.springframework.context.MessageSource; +import org.springframework.format.Formatter; + +import javax.annotation.Resource; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * Created by aalexeev on 11/22/16. + */ +public class DateFormatter implements Formatter { + @Resource + private MessageSource messageSource; + + + public DateFormatter() { + super(); + } + + @Override + public Date parse(final String text, final Locale locale) throws ParseException { + final SimpleDateFormat dateFormat = createDateFormat(locale); + return dateFormat.parse(text); + } + + @Override + public String print(final Date object, final Locale locale) { + final SimpleDateFormat dateFormat = createDateFormat(locale); + return dateFormat.format(object); + } + + private SimpleDateFormat createDateFormat(final Locale locale) { + final String format = this.messageSource.getMessage("date.format", null, locale); + final SimpleDateFormat dateFormat = new SimpleDateFormat(format); + dateFormat.setLenient(false); + return dateFormat; + } + +} -- cgit v1.2.3