Issue
How can I fix this program I use Java Spring MVC? I want to input data containing dates in the database, but it shows errors. How can I fix it?
This is my code in entity
@Entity
@Table(name ="tb_transaksi_penjualan")
public class TransaksiPenjualan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String customer;
@DateTimeFormat(pattern ="MM/dd/yyyy")
private Date tanggal;
private String barang;
private int jumlah;
private int harga;
private int total;
private Long customer_id;
private Long barang_id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
public Date getTanggal() {
return tanggal;
}
public void setTanggal(Date tanggal) {
this.tanggal = tanggal;
}
public String getBarang() {
return barang;
}
public void setBarang(String barang) {
this.barang = barang;
}
public int getJumlah() {
return jumlah;
}
public void setJumlah(int jumlah) {
this.jumlah = jumlah;
}
public int getHarga() {
return harga;
}
public void setHarga(int harga) {
this.harga = harga;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public Long getCustomer_id() {
return customer_id;
}
public void setCustomer_id(Long customer_id) {
this.customer_id = customer_id;
}
public Long getBarang_id() {
return barang_id;
}
public void setBarang_id(Long barang_id) {
this.barang_id = barang_id;
}
}
This is my code from controller
@Controller
@RequestMapping("/transaksipembelian/save")
public class TransaksiPembelianSaveController {
@Autowired
private TransaksiPembelianRepository transaksiPembelianRepository;
@ModelAttribute("transaksipembelian")
private TransaksiPembelian getTransaksiPembelian(@RequestParam(value="id", required = false) Long id)
{
if(id==null)
return new TransaksiPembelian();
else
return transaksiPembelianRepository.findById(id).orElse(null);
}
@RequestMapping(method = RequestMethod.GET)
public String form() {
return "tambahtransaksipembelian";
}
@RequestMapping(method = RequestMethod.POST)
public String submit(@ModelAttribute("transaksipembelian") TransaksiPembelian transaksipembelian ) {
System.out.println(transaksipembelian.toString());
transaksiPembelianRepository.save(transaksipembelian);
return "redirect:/transaksipembelian";
}
}
This is message from error:
Field error in object 'transaksipenjualan' on field 'tanggal': rejected value [2019-07-15]; codes [typeMismatch.transaksipenjualan.tanggal,typeMismatch.tanggal,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [transaksipenjualan.tanggal,tanggal]; arguments []; default message [tanggal]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'tanggal'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2019-07-15'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2019-07-15]]]
Solution
You have provided the date pattern for tanggal
field as MM/dd/yyyy
but you are supplying 2019-07-15
. Either change the pattern to yyyy-MM-dd
or the supplied value to 07/15/2019
.
Answered By - Kartik