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

前端js日期控件传时间类型到java后台转Date类型

程序员文章站 2022-11-28 16:30:15
把以下两个工具类放到项目里,然后那个接收参数的bean,参数变量前加上2个注解。 @JsonSerialize(using=DateJsonSerializer.class)...

把以下两个工具类放到项目里,然后那个接收参数的bean,参数变量前加上2个注解。

@JsonSerialize(using=DateJsonSerializer.class)

@JsonDeserialize(using=DateJsonDeserializer.class)

下面是代码:

工具类1:

package com.bdc.utils;

import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.DeserializationContext;

import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

* Created by BQ0196 on 2017/5/19.

*/

public class DateJsonDeserializer extends JsonDeserializer

{

public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)

throws IOException,JsonProcessingException

{

try

{

return format.parse(jsonParser.getText());

}

catch(Exception e)

{

System.out.println(e.getMessage());

throw new RuntimeException(e);

}

}

}

工具类2:

package com.bdc.utils;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

* Created by BQ0196 on 2017/5/19.

*/

public class DateJsonSerializer extends JsonSerializer

{

public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)

throws IOException,JsonProcessingException

{

jsonGenerator.writeString(format.format(date));

}

}

接收参数的bean:

package com.bdc.model.entity;

import com.bdc.utils.DateJsonDeserializer;

import com.bdc.utils.DateJsonSerializer;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.Date;

/**

* Created by lenovo on 2017/5/10.

*/

public class AppVersion {

private String appVersionId; //版本号主键

private String channelId;//渠道主键

private String channelCode;//渠道编码

private String appVersion;//版本号

private String createMan;//创建人

@JsonSerialize(using=DateJsonSerializer.class)

@JsonDeserialize(using=DateJsonDeserializer.class)

private Date createDate;//创建时间

private Date updateDate;//修改时间

private CaptureConfig captureConfig;//抓取配置表

private String captureConfigId;

private String channelName;

public String getCaptureConfigId() {

return captureConfigId;

}

public void setCaptureConfigId(String captureConfigId) {

this.captureConfigId = captureConfigId;

}

public String getChannelName() {

return channelName;

}

public void setChannelName(String channelName) {

this.channelName = channelName;

}

public CaptureConfig getCaptureConfig() {

return captureConfig;

}

public void setCaptureConfig(CaptureConfig captureConfig) {

this.captureConfig = captureConfig;

}

public String getAppVersionId() {

return appVersionId;

}

public String getCreateMan() {

return createMan;

}

public void setCreateMan(String createMan) {

this.createMan = createMan;

}

public void setAppVersionId(String appVersionId) {

this.appVersionId = appVersionId;

}

public String getChannelId() {

return channelId;

}

public void setChannelId(String channelId) {

this.channelId = channelId;

}

public String getChannelCode() {

return channelCode;

}

public void setChannelCode(String channelCode) {

this.channelCode = channelCode;

}

public String getAppVersion() {

return appVersion;

}

public void setAppVersion(String appVersion) {

this.appVersion = appVersion;

}

public Date getCreateDate() {

return createDate;

}

public void setCreateDate(Date createDate) {

this.createDate = createDate;

}

public Date getUpdateDate() {

return updateDate;

}

public void setUpdateDate(Date updateDate) {

this.updateDate = updateDate;

}

@Override

public String toString() {

return "AppVersion{" +

"appVersionId='" + appVersionId + '\'' +

", channelId='" + channelId + '\'' +

", channelCode='" + channelCode + '\'' +

", appVersion='" + appVersion + '\'' +

", createMan='" + createMan + '\'' +

", createDate=" + createDate +

", updateDate=" + updateDate +

'}';

}

}