欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springmvc 时间格式处理 博客分类: springmvc  

程序员文章站 2024-03-16 14:52:04
...

三种方式:

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)