【问题标题】:How to convert value of type 'java.lang.String' to required type 'java.util.Date'?如何将“java.lang.String”类型的值转换为所需的“java.util.Date”类型?
【发布时间】:2020-07-23 21:38:32
【问题描述】:

我的航班代码:

import java.sql.Timestamp;

import java.util.Date;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

@Entity

@Table(name = "FLIGHT")

public class Flight
{

@Column(name = "flight_number")

private String flightNumber;

@Column(name = "operating_airlines")

private String operatingAirlines;

@Column(name = "arrival_city")

private String arrivalCity;

@Column(name = "departure_city")

private String departureCity;

@Column(name = "date_of_departure")

@DateTimeFormat(pattern = "dd-MM-yyyy")

private Date dateOfDeparture;

@Column(name = "estimated_departure_time")

private Timestamp estimatedDepartureTime;

并为此包含 getter 和 setter:

在我的 Flight Repository(Interface) 中我有:

package com.nischal.flightreservation.repos;

import java.util.Date;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;

import org.springframework.data.repository.query.Param;

import com.nischal.flightreservation.entities.Flight;


public interface FlightRepository extends JpaRepository<Flight, Integer> {

@Query(value = "select * from FLIGHT where departure_city=:departureCity and 
arrival_city=:arrivalCity and date_of_departure=:dateOfDeparture", nativeQuery = true)

List<Flight> findFlights(@Param("departureCity") String from, @Param("arrivalCity") String 
to, @Param("dateOfDeparture") Date departureDate);

}

它们与数据库表正确映射。

在我的 FlightControllers 中有:

import java.util.Date;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.format.annotation.DateTimeFormat;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import com.nischal.flightreservation.entities.Flight;

import com.nischal.flightreservation.repos.FlightRepository;

@Controller

public class FlightController {

@Autowired

private FlightRepository flightRepository;

@RequestMapping("/findFlights")

public String findFlights(@RequestParam(required = false,name = "from") String from,
@RequestParam(required = false,name = "to") String to,@RequestParam(required = false, name = 
"departureDate") @DateTimeFormat(pattern = "dd-MM-yyyy") Date departureDate ,ModelMap 
modelMap) 

{

List<Flight> flights = flightRepository.findFlights(from, to, departureDate);

modelMap.addAttribute("flights",flights);

return "displayFlights";

}

}

在我的 findFlights.jsp 中有以下代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Search the Flights</title>
</head>
<body>
<form action = "findFlights" method = "post">
    <pre>
        From: <input type = "text" name = "from"/>
        To  : <input type = "text" name = "to"/>
        Departure Date:<input type = "date" name = "departureDate"/>
        <input type = "submit" value = "Search"/>
    </pre>
</form>

而 displayFlights 负责根据搜索显示可用的航班,代码为:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Available Flights</title>
</head>
<body>
<h2>Flights</h2>
<table>
    <tr>
        <th>Airlines</th>
        <th>Departure City</th>
        <th>Arrival City</th>
        <th>Departure Time</th>
    </tr>
    <c:forEach items = "${flights}" var = "flight">
    <tr>
        <td>flight.operatingAirlines</td>
        <td>flight.departureCity</td>
        <td>flight.arrivalCity</td>
        <td>flight.estimatedDepartureTime</td>
        <td><a href = "showCompleteReservation?flightId=${flight.id }">Select</a>
    </tr>
    </c:forEach>

</table>

但是每当我点击搜索按钮时,它都会产生错误提示:

如果有人想了解更多细节,我也可以提供这个项目的 github 链接。

【问题讨论】:

  • 可能这个格式不对? pattern = "dd-MM-yyyy"(在 FlightControllers 中)还有这个(在课堂上 Flight):@DateTimeFormat(pattern = "dd-MM-yyyy")
  • 没有。阿布拉我试过了。任何更多建议都会有所帮助。
  • 更详细?我真的希望少一些。 How to create a Minimal, Reproducible Example.

标签: java mysql web date


【解决方案1】:

在请求参数中,您将获得字符串,您需要一个格式化程序将其转换为可以使用 DateTimeFormatter 的日期

@PostMapping("/date")
public void date(@RequestParam("date") 
  @DateTimeFormat(pattern = "dd.MM.yyyy") Date date) {
    // ...You need to map your java date to SQL date 

或者您可以创建一个格式化程序类并使用它。

处理后

在休眠中有时间注释来做到这一点 这是一个示例

@Temporal(TemporalType.DATE)
private java.util.Date creationDate

【讨论】:

    【解决方案2】:

    我可以在错误页面中读取“for value '2020-02-02'....”。

    您已使用注释 @DateTimeFormat(pattern = "dd-MM-yyyy") 在 POJO 中正确设置了模式。但是,默认情况下,JSP 使用“yyyy-MM-dd”。 Here你可以读到无法更改格式。

    我认为html中的输入格式将取决于本地语言浏览器配置。

    如果你使用@Temporal 注解,就像Karanjot 说的那样,它会自动将java 日期映射到sql 日期。但默认格式仍然是 yyyy-MM-dd。您需要两者:时间和格式注释。

    问题是,在您的“前面”,您使用的是“yyyy-MM-dd”,而您的 findFlights 方法需要“dd-MM-yyyy”。

    我观察到您是直接从控制器调用存储库。不好的做法。在控制器和存储库之间创建服务。您的控制器将收到“yyyy-MM-dd”。将其转换并以正确的格式发送到服务。

    为了避免 findFlights 方法(和其他方法)的参数过多,请考虑使用对象和 DTO。

    【讨论】:

      【解决方案3】:

      我必须在模型类 Flight 中导入两个类,即 Temporal 和 TemporalType。 这是完整的解决方案。

      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      
      @Temporal(TemporalType.DATE)
      private Date dateOfDeparture;
      

      【讨论】:

        猜你喜欢
        • 2017-01-28
        • 2019-08-23
        • 2018-08-21
        • 2014-07-05
        • 1970-01-01
        • 2017-05-06
        • 2015-03-29
        • 2021-06-26
        • 1970-01-01
        相关资源
        最近更新 更多