使用 Spring 休息服务时日期时间错误

Getting wrong time in Date while using Spring Rest Service

这是我的pojoclass

public class TeTripCarDtl implements Serializable {

    private static final long serialVersionUID = -7601044160087552575L;

    @Id
    @Column(name = "CAR_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long carId;

    @Column(name = "TRIP_ID")
    private long tripId;

    @Column(name = "VEHICLE_TYPE")
    private String vehicleType;

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "PICKUP_DATE_TIME")
    private Date pickUpDateTime;// Here I am getting wrong time value

    @Temporal(value = TemporalType.TIMESTAMP)
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @Column(name = "RETURN_DATE_TIME")
    private Date returnDateTime;// Here I am getting wrong time value

    @Column(name = "PICK_UP_LOCATION")
    private String pickUpLocation;

    @Column(name = "DROP_OFF_LOCATION")
    private String dropOffLocation;

    @Column(name = "CONFIRMED_SPECIAL_EQUIP")
    private String confirmedSpecialEquip;

    @Column(name = "LAST_UPDATED_BY")
    private String lastUpdatedBy;

    @Temporal(TemporalType.DATE)
    @Column(name = "LAST_UPDATED_ON")
    private Date lastUpdatedOn;

    @Temporal(TemporalType.DATE)
    @Column(name = "BOOKING_DATE")
    private Date bookingDate;

    @Column(name = "STATUS")
    private String status;

    public long getCarId() {
        return carId;
    }

    public void setCarId(long carId) {
        this.carId = carId;
    }

    public long getTripId() {
        return tripId;
    }

    public void setTripId(long tripId) {
        this.tripId = tripId;
    }

    public String getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(String vehicleType) {
        this.vehicleType = vehicleType;
    }

    public Date getPickUpDateTime() {
        return pickUpDateTime;
    }

    public void setPickUpDateTime(Date pickUpDateTime) {
        this.pickUpDateTime = pickUpDateTime;
    }

    public Date getReturnDateTime() {
        return returnDateTime;
    }

    public void setReturnDateTime(Date returnDateTime) {
        this.returnDateTime = returnDateTime;
    }

    public String getPickUpLocation() {
        return pickUpLocation;
    }

    public void setPickUpLocation(String pickUpLocation) {
        this.pickUpLocation = pickUpLocation;
    }

    public String getDropOffLocation() {
        return dropOffLocation;
    }

    public void setDropOffLocation(String dropOffLocation) {
        this.dropOffLocation = dropOffLocation;
    }

    public String getConfirmedSpecialEquip() {
        return confirmedSpecialEquip;
    }

    public void setConfirmedSpecialEquip(String confirmedSpecialEquip) {
        this.confirmedSpecialEquip = confirmedSpecialEquip;
    }

    public String getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(String lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    public Date getLastUpdatedOn() {
        return lastUpdatedOn;
    }

    public void setLastUpdatedOn(Date lastUpdatedOn) {
        this.lastUpdatedOn = lastUpdatedOn;
    }

    public Date getBookingDate() {
        return bookingDate;
    }

    public void setBookingDate(Date bookingDate) {
        this.bookingDate = bookingDate;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "TeTripCarDtl [carId=" + carId + ", tripId=" + tripId + ", vehicleType=" + vehicleType
                + ", pickUpDateTime=" + pickUpDateTime + ", returnDateTime=" + returnDateTime + ", pickUpLocation="
                + pickUpLocation + ", dropOffLocation=" + dropOffLocation + ", confirmedSpecialEquip="
                + confirmedSpecialEquip + ", lastUpdatedBy=" + lastUpdatedBy + ", lastUpdatedOn=" + lastUpdatedOn
                + ", bookingDate=" + bookingDate + ", status=" + status + "]";
    }

}

输入json

    {"vehicleType":"ECAR","pickUpDateTime":"2017-06-10T07:30:04", "returnDateTime":"2017-06-10T07:30:04","pickUpLocation":"PNQ","dropOffLocation":"BOM","confirmedSpecialEquip":"HCL,TCS,INFO","status":"BOOKED"}    

Spring 休息控制器 class

@RestController
public class DateControllerTest {

    @RequestMapping(value="date_test", method = RequestMethod.POST)
    public String reciveData(@RequestBody TeTripCarDtl teTripCarDtl){
        System.out.println("PickUpDateAndTime:"+teTripCarDtl.getPickUpDateTime()+","
                + "ReturnDateAndTime:"+teTripCarDtl.getReturnDateTime());
        return "recived";
    }
}

我正在控制台中打印日期值,它是这样打印的。这里我得到的时间是错误的,我想得到时间 07:30:04 但我得到 13:00:04 除了这一切都很好

    PickUpDateAndTime:Sat Jun 10 13:00:04 IST 2017,ReturnDateAndTime:Sat Jun 10 13:00:04 IST 2017

请帮我解决这个问题。

我之前遇到过同样的问题,是时区问题,所以我在发送前将我的日期转换为字符串,你可以使用这个函数将日期转换为字符串:

public static String dateFormatString(Date date, String pattern) {
    if (date != null) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }
    return null;
}

您可以使用的模式:yyyy-MM-dd HH:mm:ss

@JsonFormat 注释存在时区问题。请检查 link 以获取有关问题的更多详细信息。jackson-data-bind issue 在 ObjectMapper 中覆盖时区也没有用。我已经通过如下实现自定义日期反序列化器解决了这个问题:

            @Component
            public class CustomDateDeserializer extends StdDeserializer<Date> {

                /**
                * 
                */
                private static final long serialVersionUID = 1L;
                private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // specify your specific timezone

                public CustomDateDeserializer() {
                this(null);
                }

                public CustomDateDeserializer(Class<?> vc) {
                super(vc);
                }

                @Override
                public Date deserialize(JsonParser jsonparser, DeserializationContext context)
                    throws IOException, JsonProcessingException {
                String date = jsonparser.getText();
                try {
                    return formatter.parse(date);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                }
            }

然后在 bean 属性的 setter 方法上应用反序列化器。

@JsonDeserialize(using = CustomDateDeserializer.class)
public void setReturnDateTime(Date returnDateTime) {
this.returnDateTime = returnDateTime;
}

同样,您可以实现自定义序列化程序,反之亦然。

这是一个时区问题。当您未指定时区时,杰克逊认为该日期是 UTC 时间并将其转换为您的默认区域设置 IST。如您所见,07:30:04 UTC 等同于 13:00:04 IST[​​=17=].

您可以像这样在输入 json 中指定时区 "pickUpDateTime":"2017-06-10T07:30:04.000+05:30" 因此 IST=UTC+5:30

Jackson @JsonFormat 注释具有特定的 timezone 属性。如果您指定要使用的时区,则可以解决此问题。

示例:

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "Europe/Madrid")

参考:http://fasterxml.github.io/jackson-annotations/javadoc/2.1.0/com/fasterxml/jackson/annotation/JsonFormat.html