0

I have a SQL column of the nature

 `x_date` DATE default null,

… in InnoDB.

Then my entity class is

@Entity
@Table
public class MyEntity{
//omitted codes

@Column(name="x_date")
private java.util.Date theDate;

//Omitted assessor 

}

Now I have a Controller class defined like this

@RestController
@RequestMapping("/data")
public class SomeController{

@Autowired
private TheDao dao;

@GetMapping
public List<MyEntity>getByDate(@RequestParam("date")String date){

//How do I get a java util.Date type from the date param to pass to this method?
return dao.findByTheDate(...){
}


}

How do I generate a Date from a user's input like http://localhost:8080/data?date=2001-07-14?

The jpa repo is

public interface TheDao extends JpaRepository<MyEntity, Integer>{

List<MyEntity>findByTheDate(Date date)
}
 
}

I've changed all Date types to LocalDate in my classes and interface and the controller class has this declaration

LocalDate loc=LocalDate.parse(date);

return dao.findByTheDate(loc);

But when I made a GET call to http://localhost:8080/data?date=2019-03-29 I got this stacktrace

Parameter value [%2019-03-29%] did not match expected type [java.time.LocalDate n/a)]...
k314159
  • 5,051
  • 10
  • 32
Chinedu
  • 103
  • 1
  • 10
  • What database engine? The data types vary radically between database engines. – Basil Bourque Nov 30 '22 at 18:00
  • 1
    `java.util.Date` represents a date with time of day as seen with an offset from UTC of zero hours-minutes-seconds. `java.sql.Date` *pretends* to represent a date only, but actually contains a time and offset. These classes are a terrible mess. They were supplanted years ago by the modern *java.time* classes. For date only, use `LocalDate`. – Basil Bourque Nov 30 '22 at 18:04
  • @BasilBourque Engine is `InnoDB` – Chinedu Nov 30 '22 at 18:25
  • 1
    I strongly recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead as @BasilBourque already said use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 30 '22 at 18:26
  • 1
    @OleV.V. I'll refactor – Chinedu Nov 30 '22 at 18:27
  • Those details such as database engine belong in your Question, not Comments. I added mentions in title, body, and tags this time around. – Basil Bourque Nov 30 '22 at 21:51
  • See https://stackoverflow.com/questions/29168494/how-to-convert-localdate-to-sql-date-java – k314159 Nov 30 '22 at 22:01
  • This seems to be just a Java question. MySQL (and InnoDB) need `yyyy-mm-dd` for its `DATE` datatype. It is up to you and Java to provide _that_ syntax. – Rick James Dec 02 '22 at 20:27
  • Of course MySQL requires `yyyy-mm-dd` formatted type of `Date`, but in my case, all I cared for is the `yyyy` part of the `Date` attribute. – Chinedu Dec 02 '22 at 21:07

1 Answers1

0

Since all I need is just to extract Subject by Date, & all previous attempts kept throwing exceptions, I simply reimplementd my dao method like this just to make use of SQL YEAR(DATE) function for comparison

@Query(value="Select * From Subject Where YEAR(x_date) =:year", nativeQuery=true)
List<MyEntity>findByTheDate(Integer year);

Then my RestController class's method reimplemented like this

@GetMapping("/date")
public List<MyEntity> getByDate(@RequestParam("date")String date){

//The string passed as request param is always in the form "yyyy-mm-dd"

Integer year= Integer.parseInt(date.subString(0,4));

return dao.findByTheDate(year);
}

and it worked

Chinedu
  • 103
  • 1
  • 10