localecalendartimeconverter.java
来自「pojo的mvc框架」· Java 代码 · 共 56 行
JAVA
56 行
package xyz.frame.converter;import java.text.DateFormat;import java.text.ParseException;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.Locale;import xyz.frame.LogicRequest;/** * Locale based calendar converter. * Uses the error key invalid_time if unable to parse its information. * * @author Guilherme Silveira */public class LocaleCalendarTimeConverter implements Converter { private String sessionKey = "javax.servlet.jsp.jstl.fmt.locale.session"; public LocaleCalendarTimeConverter() { } public LocaleCalendarTimeConverter(String sessionKey) { this.sessionKey = sessionKey; } public Object convert(String value, Class<?> type, LogicRequest context) throws ConversionException { Locale locale = (Locale) context.getSessionContext().getAttribute( this.sessionKey); if (locale == null) { locale = Locale.getDefault(); } DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, locale); Calendar calendar = new GregorianCalendar(); try { calendar.setTime(df.parse(value)); } catch (ParseException e) { throw new ConversionException("invalid_time", "Unable to parse string " + value, e); } return calendar; } /** * Returns the list of supported types * * @see xyz.frame.converter.Converter#getSupportedTypes() */ public Class<?>[] getSupportedTypes() { return new Class[] { Calendar.class }; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?