如何使用java和mysql在json中获得正确的日期格式

jtoj6r0c  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(260)

我使用javaspringmvc和mysql数据库,并尝试使用restapi。
问题在于日期字段,这是我的模型:

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "patient_mesure")
@JsonAutoDetect
public class PatientMesure {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column(name = "mesure_patient")
    private float mesure_patient;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "mesure", nullable = false)
    private Mesure mesure;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "patient", nullable = false)
    private Personne patient;
    @Column(name = "date")
    private Date date;
    @Column(name = "inserted", nullable = false)
    private Date inserted;
    @Column(name = "updated", nullable = false)
    private Date updated;

    // getters and setters
}

这是我的休息控制器:

import com.eheio.spring.models.PatientMesure;
import com.eheio.spring.models.Personne;
import com.eheio.spring.services.PatientMesureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class PatientMesureRestController {

    private PatientMesureService patientMesureService;

    @Autowired
    public void setPatientMesureService(PatientMesureService patientMesureService) {
        this.patientMesureService = patientMesureService;
    }

    @GetMapping(value = "/getAllMesures")
    public List<PatientMesure> listAllUsers(HttpServletRequest httpServletRequest) {
        HttpSession httpSession = httpServletRequest.getSession(true);
        Personne personne = (Personne) httpSession.getAttribute("personne");
        return patientMesureService.findMesuresByPatient(personne.getId());
    }
}

我得到的结果是:

{ id: 1, date: 1527801252000, inserted: 1527801252000, mesure_patient: 50, updated: 1527801252000}

如何获得日期字段的正确表单?像这样的 2018-05-31 21:14:12

lyr7nygr

lyr7nygr1#

设置一个自定义jsonserialize并将其格式化,simpledateformat如下:
首先创建jsondateserializer类:

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;

@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

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

然后在getter方法中:

@JsonSerialize(using=JsonDateSerializer.class)
public Date getDate() {
    return date;
}

另外,如果您使用的是java 8,那么datetimeformatter是线程安全的(正如@philippe marschall所指出的):

private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
p4tfgftt

p4tfgftt2#

你可以像下面这样使用

import com.fasterxml.jackson.annotation.JsonFormat;

    @JsonFormat(shape = JsonFormat.Shape.STRING,  pattern = "yyyy-MM-dd HH:mm:ss.SS a")
    @Column(name = "date")
    private Date date;

    @JsonFormat(shape = JsonFormat.Shape.STRING,  pattern = "yyyy-MM-dd HH:mm:ss.SS a")
    @Column(name = "inserted", nullable = false)
    private Date inserted;

    @JsonFormat(shape = JsonFormat.Shape.STRING,  pattern = "yyyy-MM-dd HH:mm:ss.SS a")
    @Column(name = "updated", nullable = false)
    private Date updated;

相关问题