Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Sunday, December 19, 2010

spring的属性编辑器CustomDateEditor及日期对象转化

在spring mvc的Controller中,属性在通过依赖注入(DI)时,普通数据类型都能够辨识。但诸如Date之类,就需要自定义属性编辑器解决。否则报如下错误:
org.springframework.beans.TypeMismatchException:
Failed to convert property value of type [java.lang.String] to required type
[java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
这表示spring无法找到合适的转换策略,需要自己写一个转换器,在spring中称之为属性编辑器。
spring中的属性编辑器可以将字符串转换为相应的对象,然后注入到其它对象中去。
编写自己的属性编辑器的步骤很简单,属性编辑器类需要从java.beans.PropertyEditorSupport类继承,在这个类中有一个setAsText方法,这个方法有一个String类型的参数,通过这个方法,可以将String类型的参数值转换成其他类型的属性。在这个方法中我们还需要使用一个setValue方法,就来指定转换后的对象实例。
spring 中有个 CustomDateEditor 的类就是继承 PropertyEditorSupport 的一个属性编辑器,在Controller中添加一个@InitBinder的Annotation到某个方法上,在方法中指明日期字符串的格式,就可以将符合此格式的字符串转化为日期对象,代码如下:


/**
* <pre>
* HTML forms work with string values only, when your Authority is a complex bean. You need to configure a PropertyEditor to perform conversion between Authority and String:
*
* @InitBinder
* public void initBinder(WebDataBinder b) {
* b.registerCustomEditor(Authority.class, new AuthorityEditor());
* }
*
* private class AuthorityEditor extends PropertyEditorSupport {
* @Override
* public void setAsText(String text) throws IllegalArgumentException {
* // 另外一个例子是根据字符串,从数据库中查找返回对象
* setValue(authorityService.findById(Long.valueOf(text)));
* }
*
* @Override
* public String getAsText() {
* return ((Authority) getValue()).getId();
* }
* }
* </pre>
*
* 这个方法用来将页面表单上传的Date字符串转化成java的Date对象
*
* @param binder
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}


Reference: how to pass a date into bean property

Thursday, August 14, 2008

javascript getTime() shortcut


>>> time1 = (new Date()).getTime(), time2 = +new Date(), time3 = new Date() * 1;
1218645767082
>>> time1
1218645767082
>>> time2
1218645767082
>>> time3
1218645767082

Friday, March 23, 2007

计算某月天数

function days_in_month($a_month, $a_year) {
return date('t', strtotime($a_year . '-' . $a_month . '-01'));
}
date :
t 给定月份所应有的天数 28 到 31

strtotime
(PHP 3 >= 3.0.12, PHP 4, PHP 5)
strtotime -- 将任何英文文本的日期时间描述解析为 Unix 时间戳
说明
int strtotime ( string time [, int now] )
本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。
本函数将使用 TZ 环境变量(如果有的话)来计算时间戳。自 PHP 5.1.0 起有更容易的方法来定义时区用于所有的日期/时间函数。此过程在 date_default_timezone_get() 函数页面中有说明。
注: 如果给定的年份是两位数字的格式,则其值 0-69 表示 2000-2069,70-100 表示 1970-2000。
参数
time
被解析的字符串,格式根据 GNU Date Input Formats 语法
now
用来计算返回值的时间戳
返回值
成功则返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前本函数在失败时返回 -1。

echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";

Monday, February 26, 2007

ruby date time parse

require 'parsedate'
res = ParseDate.parsedate("2000-01-01")
stamp = Time.local(*res).to_i
for i in 0 .. 365
time_stamp = stamp + 24 * 60 * 60 * i
t = Time.at(time_stamp)
actual_date = t.strftime("%Y-%m-%d")
day_of_week = t.strftime("%w").to_i + 1
day_of_month = t.strftime("%d").to_i
day_of_year = t.strftime("%j").to_i
month_of_year = t.strftime("%m").to_i
day_name = t.strftime("%A")
day_short_name = t.strftime("%a")
month_name = t.strftime("%B")
month_short_name = t.strftime("%b")
week_of_year = t.strftime("%W")
actual_year = t.strftime("%Y")

# quarter_of_year
quarter_of_year = (month_of_year - 1) / 3 + 1

# week_of_month
interval = 6
start_date = 0
end_date = day_of_week + 1
for n in 0 .. 5
if day_of_month >= start_date && day_of_month < end_date week_of_month = n + 1 break end start_date = end_date end_date = (n + 1) * interval + day_of_week + 1 end puts actual_date puts week_of_month puts quarter_of_year end


SELECT WEEK('1998-12-31',1);#mysql week() function