时间类型传到后台controller

[b][size=large]需要在controller里面为日期数据的从jsp传送到后台做的准备[/size][/b]

@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
// 时间格式为yyyy-MM-dd类型需要的绑定
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(df,
true));
// 时间格式为HH:mm:ss类型需要的绑定 这里我们自己定义一个CustomTimeEditor类 用来接收需要传过来的类型
SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss");
binder.registerCustomEditor(Time.class, null, new CustomTimeEditor(tf,
true));
}

[b][size=large]CustomTimeEditor类 :仿照CustomDateEditor类[/size][/b]
package com.propertyEditors;

import java.beans.PropertyEditorSupport;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.StringUtils;

public class CustomTimeEditor extends PropertyEditorSupport{

private final DateFormat dateFormat;

private final boolean allowEmpty;

private final int exactDateLength;


/**
* Create a new CustomDateEditor instance, using the given DateFormat
* for parsing and rendering.
* <p>The "allowEmpty" parameter states if an empty String should
* be allowed for parsing, i.e. get interpreted as null value.
* Otherwise, an IllegalArgumentException gets thrown in that case.
* @param dateFormat DateFormat to use for parsing and rendering
* @param allowEmpty if empty strings should be allowed
*/
public CustomTimeEditor(DateFormat dateFormat, boolean allowEmpty) {
this.dateFormat = dateFormat;
this.allowEmpty = allowEmpty;
this.exactDateLength = -1;
}

/**
* Create a new CustomDateEditor instance, using the given DateFormat
* for parsing and rendering.
* <p>The "allowEmpty" parameter states if an empty String should
* be allowed for parsing, i.e. get interpreted as null value.
* Otherwise, an IllegalArgumentException gets thrown in that case.
* <p>The "exactDateLength" parameter states that IllegalArgumentException gets
* thrown if the String does not exactly match the length specified. This is useful
* because SimpleDateFormat does not enforce strict parsing of the year part,
* not even with {@code setLenient(false)}. Without an "exactDateLength"
* specified, the "01/01/05" would get parsed to "01/01/0005". However, even
* with an "exactDateLength" specified, prepended zeros in the day or month
* part may still allow for a shorter year part, so consider this as just
* one more assertion that gets you closer to the intended date format.
* @param dateFormat DateFormat to use for parsing and rendering
* @param allowEmpty if empty strings should be allowed
* @param exactDateLength the exact expected length of the date String
*/
public CustomTimeEditor(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) {
this.dateFormat = dateFormat;
this.allowEmpty = allowEmpty;
this.exactDateLength = exactDateLength;
}


/**
* Parse the Date from the given text, using the specified DateFormat.
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (this.allowEmpty && !StringUtils.hasText(text)) {
// Treat empty String as null value.
setValue(null);
}
else if (text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
throw new IllegalArgumentException(
"Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
}
else {
try {
Date date = this.dateFormat.parse(text);
setValue(new Time(date.getTime()));
}
catch (ParseException ex) {
throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);
}
}
}

/**
* Format the Date as String, using the specified DateFormat.
*/
@Override
public String getAsText() {
Time value = (Time) getValue();
return (value != null ? this.dateFormat.format(value) : "");
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值