Based on your new comments, you may need a custom converter for swapping between Strings and LocalDateTime.
Here is the converter:
import org.springframework.core.convert.converter.Converter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public final class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
private final DateTimeFormatter formatter;
public LocalDateTimeConverter(String dateFormat) {
this.formatter = DateTimeFormatter.ofPattern(dateFormat);
}
@Override
public LocalDateTime convert(String source) {
if (source == null || source.isEmpty()) {
return null;
}
try {
return LocalDateTime.parse(source, formatter);
} catch (DateTimeParseException e) {
// Log the error here
// return null if you need the 400 error, or set a default date
return null;
}
}
}
You will need to register the converter. I am assuming you are not using Spring Boot, so you can register it in WebMvcConfigurerAdapter
like so:
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
class WebMvcContext extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new LocalDateTimeConverter("yyyy-MM-dd'T'HH:mm:ss"));
}
}
Column definition will look like this:
@Column(name = "GioBatDau")
private LocalDateTime gioBatDau;
Previous Answer and still relevant
One option to resolve the issue you're facing is to switch the column type from java.util.Date
to java.time.LocalDateTime
and then specify the correct date-time format.
Here is the snippet:
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Column;
import java.time.LocalDateTime;
public class YourEntity {
@Column(name = "GioBatDau")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime gioBatDau;
}
Why Use java.time.LocalDateTime?
Since Java 8, it's recommended to use java.time.LocalDateTime or java.time.LocalDate over java.util.Date for several reasons:
Immutability: Classes in java.time package are immutable, making them thread-safe.
Rich API: They provide a richer API for date-time manipulation.
Clearer Semantics: These classes have clearer semantics for dealing with dates, times, and time-zones.
Time Zone Handling: Easier to work with time-zones.
Controller/Service-level Conversion
While it's convenient to use @DateTimeFormat
at the entity level, it's often considered better practice to keep entity classes as clean as possible. You can perform the conversion at the service or controller level. This makes your entity classes more reusable and decouples them from the web layer of your application.
For more information on the java.time
package, you can read the official Java documentation .
gioBatDau
field? Does that represent a moment, a specific point on the time line? Or does that represent a date and a time-of-day without the context of a time zone or offset-from-UTC? (Tip: Translating all your text to English is helpful when posting here on the English edition of Stack Overflow.)LocalDateTime
in Java anddatetime
in MySQL (JavaDate
is ill suited).