2016-01-13 53 views
2

我正在写一个休息服务,我得到的答复格式为“1448994600000”,但我需要它给予日期,月份,年份格式的回复。如何在春季休眠中反序列化日期格式?

如果我发送给予2015年12月2日的数据格式,它给我的错误

客户端发送的请求是语法不正确

在回答我碰到一个“12333333333”的形式,我需要它来回应12-2-2015

我已经使用下面的代码来反序列化它,

import java.io.Serializable; 
import java.sql.Date; 
import java.sql.Timestamp; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

import org.codehaus.jackson.annotate.JsonAutoDetect; 
import org.codehaus.jackson.annotate.JsonIgnoreProperties; 
import org.codehaus.jackson.map.annotate.JsonSerialize; 


@JsonAutoDetect 
@Entity 
@Table(name = "DataValueTable") 
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
public class DataValueTable implements Serializable { 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue 
@Column(name = "ID") 
private long id; 

@JsonSerialize(using=JsonDateSerializer.class) 
@Column(name = "Time") 
private Date time; 

public Date getTime() { 
    return time; 
} 

public void setTime(Date time) { 
    this.time = time; 
} 

JsonDateSerializer.java

package com.beingjavaguys.model; 

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; 
import org.springframework.stereotype.Component; 
/** 
* Used to serialize Java.util.Date, which is not a common JSON 
* type, so we have to create a custom serialize method;. 
* 
* @author Loiane Groner 
* http://loianegroner.com (English) 
* http://loiane.com (Portuguese) 
*/ 
@Component 
public class JsonDateSerializer extends JsonSerializer<Date>{ 
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
@Override 
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) 
throws IOException, JsonProcessingException { 
String formattedDate = dateFormat.format(date); 
gen.writeString(formattedDate); 
} 
} 

方法在我的控制器

更新数据

@RequestMapping(value = "/updateData", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 
    public @ResponseBody 
    Status updateData(@RequestBody DataValueTable dataObject) { 
     try { 
    //response.addHeader("Access-Control-Allow-Origin","*"); 
     dataServices.insertData(dataObject); 
     return new Status(1, "Data updated Successfully !"); 
     } catch (Exception e) { 
     // e.printStackTrace(); 
     return new Status(0, e.toString()); 
     } 
     } 

回答

1

您可使用已被引入到java8实施导入特定库

import java.time.LocalDate 
import java.time.ZoneId 
import java.time.format.DateTimeFormatter 

LocalDate localDate= LocalDate.now(); 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); 
String stamp = localDate.format(formatter); 

如果需要从Date转换,你会做如下

Date input = new Date(); 
LocalDate localDate = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); 

和输出可能是某事像13-01-2016

+0

你能告诉我我应该如何修改我的发布代码,以便我可以实现正常的日期格式。 – dafodil

+0

您是否尝试过调试? –

+0

不,我没有调试,在这里我没有得到上述代码在我的code.should我应该修改JsonDateSerializer.java文件与您的代码 – dafodil