Java 过账日期序列化为错误日期,并有1天休息

Java 过账日期序列化为错误日期,并有1天休息,java,java-time,Java,Java Time,我将数据departureTime:“2019-10-21”发布到我的端点,并在spring程序中输出收到的departureTime变量 @CrossOrigin(origins = "*") @RequestMapping(value="/travel/query", method= RequestMethod.POST) public ArrayList<TripResponse> query(@RequestBody QueryInfo info,@RequestHeader

我将数据
departureTime:“2019-10-21”
发布到我的端点,并在spring程序中输出收到的departureTime变量

@CrossOrigin(origins = "*")
@RequestMapping(value="/travel/query", method= RequestMethod.POST)
public ArrayList<TripResponse> query(@RequestBody QueryInfo info,@RequestHeader HttpHeaders headers){

    if(info.getStartingPlace() == null || info.getStartingPlace().length() == 0 ||
            info.getEndPlace() == null || info.getEndPlace().length() == 0 ||
            info.getDepartureTime() == null){
        System.out.println("[Travel Service][Travel Query] Fail.Something null.");
        ArrayList<TripResponse> errorList = new ArrayList<>();
        return errorList;
    }
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    Calendar ca = Calendar.getInstance();
    ca.setTime(info.getDepartureTime());
    System.out.println("Departure date is " + format.format(ca.getTime()));

    ...
}


public class QueryInfo {

    @Valid
    @NotNull
    private String startingPlace;

    @Valid
    @NotNull
    private String endPlace;

    @Valid
    @NotNull
    private Date departureTime;

    public QueryInfo(){
        //Default Constructor
    }

    public String getStartingPlace() {
        return startingPlace;
    }

    public void setStartingPlace(String startingPlace) {
        this.startingPlace = startingPlace;
    }

    public String getEndPlace() {
        return endPlace;
    }

    public void setEndPlace(String endPlace) {
        this.endPlace = endPlace;
    }

    public Date getDepartureTime() {
        return departureTime;
    }

    public void setDepartureTime(Date departureTime) {
        this.departureTime = departureTime;
    }
}
它输出
sun.util.calendar.ZoneInfo[id=“America/Los_Angeles”…
。我认为这是正确的。

calendar.getInstance()
将根据服务器托管的位置设置时区。因此,如果您不在UTC-7时区,您将看到时间上的差异

解决此问题的最佳方法通常是使用特定时区(如UTC)或用于数据的任何其他时区。然后,您可以获得该特定时区的
日历,如下所示:

Calendar.getInstance(TimeZone.getTimeZone("UTC"))

避免使用
SimpleDateFormat
Calendar
,而是使用
ZoneDateTime

例如:

ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
样本输出:

2019-10-22
或者,如果您想继续使用
java.util
包中的旧API,那么您必须使用:

ca.setTimeZone(TimeZone.getTimeZone("UTC")); // to set the time zone for using calendar API.
format.setTimeZone(TimeZone.getTimeZone("UTC")); // set timezone for formatter.

另一个解决方案(虽然不是聪明的,但很有效)可以是,您在
DTO
中将
dateField
定义为字符串字段。因此,您发送给您的内容将得到相同的结果,没有基于时区或其他内容的日期转换。然后您可以将该字符串转换为您喜欢的日期
格式/timezone

如何定义
departureTime
?有任何注释吗?这几乎是免费的当然是时区问题。Calender.getTimeZone()是否与您预期的时区匹配?@Anish您在我的问题标题中所做的编辑让我感到困惑。“标题中的日期显示错误”,它表明标题显示了错误的日期,而在我的问题标题中,标题是输入,不可能是错误的。我将还原它。@Andreas它没有任何注释。在任何情况下,这都是一个很好的建议(或者如果一天中的时间不重要,可能是
LocalDate
)!我不确定这是否足以解决提问者的问题。@OleV.V.是的,我同意你的看法。如果OP告诉你使用此选项后会发生什么,那么他的问题就有可能得到解决。:)我的输入没有指定时区。当我的应用程序收到日期字符串时,它能解释为没有时区的本地时间吗?我可以无法将“离开时间”的类型从Date更改为LocalDate,因为它会影响系统的所有部分。@Gqqnbig您可能可以设置时区-
ca.setTimeZone(timezone.getTimeZone(“UTC”)
。明白了。看来这只是一个显示错误,我必须设置
format.setTimeZone(timezone.getTimeZone(“UTC”))
。日期中的值本身是正确的。
ca.setTimeZone(TimeZone.getTimeZone("UTC")); // to set the time zone for using calendar API.
format.setTimeZone(TimeZone.getTimeZone("UTC")); // set timezone for formatter.