springmvc 时间格式处理 博客分类: springmvc
三种方式:
1,局部处理
标签
注解
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
/**
* @description 自定义返回JSON 数据格中日期格式化处理
* @author aokunsang
* @date 2013-5-28
*/
public class CustomDateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date value,
JsonGenerator jsonGenerator,
SerializerProvider provider)
throws IOException, JsonProcessingException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
jsonGenerator.writeString(sdf.format(value));
}
}
@JsonSerialize(using = CustomDateSerializer.class)
public Date getCreateDate() {
return createDate;
}
(2)@datetimeformate()
mapper.xml配置文件中的类型改为datedemic
2,全局处理
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true" >
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.esteel.until.CustomJacksonObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
package com.esteel.until;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
/**
* 设置json到java对象互相转换时的规则
* @author wanglei
*/
public class CustomJacksonObjectMapper extends ObjectMapper {
public CustomJacksonObjectMapper(){
super();
this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
this.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
this.setSerializationInclusion(Include.ALWAYS);
this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//json中存在字段,但JAVA中没有的
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule module = new SimpleModule();
module.addSerializer(new DealDate(Date.class));
// module.addSerializer(new JsonDoubleSerializer(Double.class));
this.registerModule(module);
}
class DealDate extends JsonSerializer<Date> {
public DealDate(Class<Date> date) {
super();
}
public Class<Date> handledType() {
return Date.class;
}
@Override
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
jgen.writeString(sdf.format(value));
}
}
}
3,jsp页面处理
<td><fmt:formatDate value="${item.chkTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
4,java处理
public static final String PATTERN_DATE = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DATE);
一般有3,4处理即可
数据库处理
数据字段类型:
CHK_TIME : DATE
SELECT C.* FROM TB_CUS_FIRM_CHG C WHERE 1 = 1 AND to_char(C.CHK_TIME, 'yyyy-mm-dd') >= ? AND to_char(C.CHK_TIME, 'yyyy-mm-dd') <= ? order by C.CUSTOMER_KEY asc
Parameters: 2008-06-01(String), 2016-06-20(String), 20(Integer), 0(Integer)
上一篇: 如何成为大数据 高手 博客分类: DATABASE 大数据论文
下一篇: Java并发编程:Callable、Future和FutureTask 博客分类: Java多线程 CallableFutureFutureTask
推荐阅读
-
Spring MVC中Controller如何将数据返回给页面 博客分类: springmvc springmvc
-
xml中遍历map参数 博客分类: springmvc
-
springmvc页面时间格式的处理 博客分类: springmvc springmvc
-
SpringMVC工作原理概述 博客分类: springmvc springMVC
-
springMvc直接自动转化对象为json的配置(不需要自己转成json) 博客分类: springmvc springMvc
-
springmvc 时间格式处理 博客分类: springmvc
-
spring mvc 用map的形式接收form表单的参数: 博客分类: springmvc
-
Spring MVC 原理探秘 - 容器的创建过程 博客分类: springmvc springmvc
-
《项目实战》从Spring开始说起 博客分类: 项目实战 spring项目实战springmvc
-
springmvc RestTemplate工具类 博客分类: JavaSpring springmvcRestTemplateUtil