📄 localecalendartimeconverter.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -