DateTimeConverter.java
1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.thinkgem.jeesite.common.xstream;
import java.util.Date;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
/**
* XStream 日期转换类
* @author WangZhen
*/
public class DateTimeConverter implements Converter {
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
Date date = (Date) source;
if (date != null){
writer.setValue(DateUtils.formatDateTime(date));
}else{
writer.setValue("");
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
try{
Date date = DateUtils.parseDate(reader.getValue());
return date;
}catch (Exception e) {
return null;
}
}
@SuppressWarnings("rawtypes")
public boolean canConvert(Class type) {
return type.equals(Date.class);
}
}