springmvc日期格式化

这里因为自己也是因为这个东西花了挺多时间的,虽然没有深入研究,但是也记录一下。做个只是积累吧。

  • 1.格式化注解(这个大部分都知道了)
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
  • 2.controller/controller基类中使用数据绑定方法initBinder
@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
  • 3.注册一个全局日期类型转化器
<!-- 类型转换支持 -->
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.gss.common.converter.StringDateConverter" />
                <bean class="com.gss.common.converter.DateStringConverter" />
            </list>
        </property>
    </bean>
    <!-- 支持spring mvc新的注解类型 详细spring3.0手册 15.12.1 mvc:annotation-driven -->
    <mvc:annotation-driven conversion-service="conversionService" />

之后的表示conversion-service替换为conversionService这个bean来处理

/**
 * 
 * 日期转换基类
 *
 * @author gonghb
 * @date 2016年11月17日
 *
 */
public class DateConverterBase {
    private String datePattern = "yyyy-MM-dd";
    private String timePattern = "HH:mm:ss";
    private DateFormat dateFormat = new SimpleDateFormat(datePattern);
    private DateFormat dateTimeFormat = new SimpleDateFormat(datePattern + " " + timePattern);

    public DateFormat getDateFormat() {
        return dateFormat;
    }

    public void setDateFormat(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    /**
     * @return the dateTimeFormat
     */
    public DateFormat getDateTimeFormat() {
        return dateTimeFormat;
    }

    /**
     * @param dateTimeFormat the dateTimeFormat to set
     */
    public void setDateTimeFormat(DateFormat dateTimeFormat) {
        this.dateTimeFormat = dateTimeFormat;
    }

}
/**
 * 
 * 字符串转日期
 *
 * @author gonghb
 * @date 2016年11月17日
 *
 */
public class StringDateConverter extends DateConverterBase implements
        Converter<String, Date> {

    /**
     * 
     * 将字符串转换为日期
     *
     * @param source
     *            日期字符串
     * @return Date 日期
     */
    public Date convert(String source) {
        if (source == null) {
            return null;
        }
        String trim = source.trim();
        if (trim.length() == 0) {
            return null;
        }
        try {
            return source.contains(":") ? getDateTimeFormat().parse(trim)
                    : getDateFormat().parse(trim);
        } catch (ParseException e) {
            return null;
        }
    }

}
/**
 * 
 * 日期转字符串类
 *
 * @author gonghb
 * @date 2016年11月17日
 *
 */
public class DateStringConverter extends DateConverterBase implements Converter<Date, String> {

    /**
     * 
     * 将日期转换为字符串
     *  
     * @param source 日期
     * @return String 日期字符串
     */
    public String convert(Date source) {
        if (source == null){
            return "";
        }
        return getDateFormat().format(source);
    }
}

这个还没理解透,因为我们项目里面使用了2.3。但是并没有处理到全局的Converter中,起作用的是initBinder,知道的童鞋可以告诉我ヾ(o◕∀◕)ノヾ

从后端Date类型到jsp页面中的日期转换

-JSP模版引擎方法:

 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>   
    <fmt:formatDate value="${obj.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值