Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format of DateTime field not recognized by Spring

I am developing web application using Spring 3.2.4. I have some forms with fields containing date and time. Piece of my jsp:

<form:form method="post" action="/add" modelAttribute="licence">
    ...
    <form:input type="datetime" path="beginDate"/>
    <form:input type="datetime" path="endDate"/>
    <form:input path="quantityLimit"/>
    ...
</form:form>

Normal form, nothing fancy. I am using datepicker, which gives me date in format yyyy-MM-dd HH:mm, so I've added this to my controller:

@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    dateFormat.setLenient(true);
    webDataBinder.registerCustomEditor(DateTime.class, new CustomDateEditor(dateFormat, true));
}

Also, I have added <mvc:annotation-driven/> to my servlet configuration xml, as stated on some blogs.

There's target controller:

@RequestMapping(value = "/{softwareId}/licence/add", method = RequestMethod.POST)
public String addLicence(@PathVariable("softwareId") Long softwareId, Licence licence, Model model) {
    Software software = softwareRepository.findOne(softwareId);
    licence.setSoftware(software);
    licenceRepository.save(licence);
    return ADMIN_PATH + "softwareEdit";
}

And software class looks like this:

@Entity
@Table(name = "licences")
public class Licence {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "begin_date")
private DateTime beginDate;

@Column(name = "end_date")
private DateTime endDate;

@Column(name = "quantity_limit")
private Long quantityLimit;

@ManyToOne
private Software software;

//getters, setters, etc.
}

The problem is: when I submit my form with dateTime field empty it works perfectly, but when I have anything in date field (no matter if it's properly formatted or not) I get HTTP Error 400: Bad Request. No exceptions in console, only bad request, but I am pretty sure it has something to do with date parsing.

Is there a well described method of dealing with date and time fields in forms in Spring applications?

like image 700
Crazy Yoghurt Avatar asked Mar 19 '14 21:03

Crazy Yoghurt


People also ask

How do I get the current time in spring boot?

getTime(); DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String formattedDate=dateFormat. format(date); System. out. println("Current time of the day using Calendar - 24 hour format: "+ formattedDate); } . . .

How do I extract data between two dates in spring boot?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.


1 Answers

Make your life simple and use @DateTimeFormat, getting rid of your WebDataBinder configuration. It seems CustomDateEditor only works with java.util.Date and Spring has no other (default/not-specified) mechanism to convert from a String to a DateTime.

@DateTimeFormat is such a mechanism.

@Column(name = "begin_date")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private DateTime beginDate;
like image 67
Sotirios Delimanolis Avatar answered Oct 03 '22 10:10

Sotirios Delimanolis