注解日期时间转换类使用到了日期工具类DateUtils
package org.sunflower.common.web.converter; import java.text.ParseException; import java.util.Date; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.metainfo.Annotation; import org.zkoss.zk.ui.sys.ComponentCtrl; import org.zkoss.zkplus.databind.TypeConverter; import org.sunflower.common.util.CheckExceptionConverter; import org.sunflower.common.util.DateUtils; /** * 日期时间转换函数 * <p> * {@link #coerceToUi(Object, Component)}支持的格式为 * <ol> * <li>datetime:yyyy-MM-dd HH:mm:ss</li> * <li>date:yyyy-MM-dd</li> * <li>time:HH:mm:ss</li> * <li>自定义:模式规则与<code>java.text.SimpleDateFormat</code>pattern相同</li> * </ol> * * 例如:默认使用日期时间模式 * <p> * <ol> * <li>日期时间模式:<label value="@{account.createTime,converter='org.sunflower.common.web.zk.converter.DatetimeConverter'}" * self="@{format('datetime')}"/> * <li>日期模式:<label value="@{account.createTime,converter='org.sunflower.common.web.zk.converter.DatetimeConverter'}" * self="@{format('date')}"/> * <li>时间模式:<label value="@{account.createTime,converter='org.sunflower.common.web.zk.converter.DatetimeConverter'}" * self="@{format('time')}"/> * <li>自定义模式:<label value="@{account.createTime,converter='org.sunflower.common.web.zk.converter.DatetimeConverter'}" * self="@{format('yyyy年MM月dd日 HH时mm分ss秒')}"/> * </ol> * * @author sunflower */ public class DatetimeConverter implements TypeConverter { public static final String PATTERN_DATETIME = "datetime"; public static final String PATTERN_DATE = "date"; public static final String PATTERN_TIME = "time"; private static final Logger log = LoggerFactory .getLogger(DatetimeConverter.class); @Override public Object coerceToBean(Object strDatetime, Component comp) { // 忽略null string if (strDatetime == null) { return ""; } // 忽略非String类型日期格式,并错误警告 if (!(strDatetime instanceof String)) { log.error("组件{}绑定的对象应为String类型,您使用的类型是{}!", comp.getClass() .getName(), strDatetime.getClass().getName()); return ""; } String date = (String) strDatetime; final Annotation annot = ((ComponentCtrl) comp).getAnnotation("format"); String pattern = null; try { if (annot != null) { pattern = annot.getAttribute("value"); if (StringUtils.equalsIgnoreCase(PATTERN_DATETIME, pattern)) { return DateUtils.parseDatetime(date); } else if (StringUtils.equalsIgnoreCase(PATTERN_DATE, pattern)) { return DateUtils.parseDate(date); } else if (StringUtils.equalsIgnoreCase(PATTERN_TIME, pattern)) { return DateUtils.parseTime(date); } else { return DateUtils.parseDatetime(date, pattern); } } return DateUtils.parseDatetime(date); } catch (ParseException e) { throw CheckExceptionConverter.convertToRuntimeException(e); } } @Override public Object coerceToUi(Object dt, Component comp) { // 忽略null bean,并警告 if (dt == null) { log.warn("组件" + comp.getClass().getSimpleName() + "绑定的日期为null!"); return ""; } // 忽略非日期类型对象,并错误警告 if (!(dt instanceof Date)) { log.error("组件{}绑定的对象应为java.util.Date类型或者其泛化类,您帮的类型是{}!", comp .getClass().getName(), dt.getClass().getName()); return ""; } Date date = (Date) dt; final Annotation annot = ((ComponentCtrl) comp).getAnnotation("format"); String pattern = null; if (annot != null) { pattern = annot.getAttribute("value"); if (StringUtils.equalsIgnoreCase(PATTERN_DATETIME, pattern)) { return DateUtils.formatDatetime(date); } else if (StringUtils.equalsIgnoreCase(PATTERN_DATE, pattern)) { return DateUtils.formatDate(date); } else if (StringUtils.equalsIgnoreCase(PATTERN_TIME, pattern)) { return DateUtils.formatTime(date); } else { return DateUtils.formatDatetime(date, pattern); } } return DateUtils.formatDatetime(date); } }