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

Springmvc 注入字符串与时间格式的转换

程序员文章站 2022-07-10 16:19:12
...
以下列出两种spring支持的时间转换
-、方式一
   1、字符串转时间
   
 
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date startTime;

   2、字符串转时间亦可以,这个加载controller控制层即可
  
@InitBinder  
public void initBinder(WebDataBinder binder) {  
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
dateFormat.setLenient(false);  
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 

  3、时间转字符串
  @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    private Date startTime;
二、方式二(序列化)
   JsonSerialize:时间为字符串格式
   JsonDeserialize :字符串转时间
  
    @JsonSerialize(using = JsonDateSerializer$19.class)
    @JsonDeserialize(using = JsonDateDeserializer$19.class)
    private Date updateTime;

  
package com.ppfuns.mediaasset.core.common;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class JsonDateSerializer$19 extends JsonSerializer<Date> {

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
            JsonProcessingException {
        String formattedDate = formatter.format(value);
        jgen.writeString(formattedDate);
    }
}
  

 
package com.ppfuns.mediaasset.core.common;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TODO: 字符串转时间格式为yyyy-MM-dd HH:mm:ss
 * 
 * @author jiandequn
 * @date 2014-6-24 上午10:07:19
 * @version 0.1.0 
 * @copyright yougou.com 
 */
public class JsonDateDeserializer$19 extends JsonDeserializer<Date> {

	private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
		String str = jp.getText().trim();
		if (StringUtils.isEmpty(str)) {
			return null;
		}
		try {
			return formatter.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

}
 
MAVEN依赖的包
   <dependency>
 3             <groupId>org.codehaus.jackson</groupId>
 4             <artifactId>jackson-mapper-asl</artifactId>
 5             <version>${jackson.version}</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>com.fasterxml.jackson.core</groupId>
 9             <artifactId>jackson-core</artifactId>
10             <version>${jackson.core.version}</version>
11         </dependency>
12         <dependency>
13             <groupId>com.fasterxml.jackson.core</groupId>
14             <artifactId>jackson-databind</artifactId>
15             <version>${jackson.core.version}</version>
16         </dependency>
17 
18 
19 <properties>
20         <jackson.version>1.9.13</jackson.version>
21         <jackson.core.version>2.4.2</jackson.core.version>
22 </properties>

  
相关标签: maven java spring