getting data from datapicker to entity

77 Views Asked by At

Given this Entity I need to post the new Object of type Contracts. I http://www.springframework.org/tags/form tag form/sf. I also got jquery datapicker. The problem I have got is datapicker returns a String not Date object. How can it be parsed? The only solution I think will work is to get date from datepicker as @RequestParam in the @Controller class and parse it as a java.util.Date object.

@Entity
@Table(name = "contract")
public class Contracts {
    @Id
    @Column(name = "contract_id")
    private int contractId;
    @Column(name = "date_added")
    private Date creationDate;
    @Column(name = "date_start")
    private Date startDate;
    @Column(name = "date_end")
    private Date finishDate;
    @NotNull
    @Column(name = "payment_amount")
    private Integer paymentAmount;
    @Column(name = "payment_type")
    @Enumerated(EnumType.STRING)
    private PaymentType paymentType;
    private boolean valid;
    @ManyToOne
    @JoinColumn(name = "system_id")
    private Systems system;

And this is 'POST' part of my Controller class.

@RequestMapping(value = "/createcontract", method = RequestMethod.POST)
public String createContract(@ModelAttribute("contract") @Valid final Contracts contract, BindingResult results,
        @RequestParam("system-id") int systemId) {
    if(results.hasErrors())
        return "newcontract";
    return "redirect:contracts";

Another question (because I tagged postgresql) is whether is 'all-right' to store java.util.Data object in postgresql as just date or maybe I should store it as 'timestamp with time zone'?

2

There are 2 best solutions below

0
andreim On

jQuery UI DatePicker gives Date object

The plugin allows you to obtain a Date object using getDate method.

Here is a Plunker with a working example.

$(function() {
  $('#datePicker').datepicker();
  $('#datePicker').on('change', () => {
      let date = $('#datePicker').datepicker('getDate');
      alert(`Date ${date} is ${date instanceof Date ? 'a Date object' : 'not a Date object'}`);
  });
});

Date type - JPA to PostgreSQL mapping

  1. Use timestamp with time zone in PostgreSQL

Usually is best to keep your dates as UTC in your database. This will give the most flexibility as you can represent the date in whichever zone you desire. This translates into using the timestamp with time zone as a type which will store as UTC. This means that your date time, before being stored, is added the zone offset in order to obtain the UTC.

  1. Set the timezone to UTC of your JVM or your JPA provider

Is important that all your dates are expressed as UTC and not in a specific time zone. And when these are sent via the wire to your database the session needs to have the UTC time zone.

This means that the timezone needs to be UTC on JVM or JPA provider (eg: Hibernate).

As explained in this blog you can set UTC by:

JVM: java -Duser.timezone=UTC -jar blabla.jar

Or by

JVM: TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));

Or by

Hibernate: (application.properties) spring.jpa.properties.hibernate.jdbc.time_zone = UTC

Check which version works for you.

  1. Persisting Date field

Therefore your field can be expressed as:

@Column(name = "date_added", columnDefinition = "TIMESTAMP WITH TIMEZONE")
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;

And you need to ensure that the Date object represents a date time in UTC.

  1. Useful mappings

You can store your Date object in PostgreSQL as:

  • timestamp with or without timezone
  • time with or without timezone
  • date

Using the JPA annotation @Temporal applied on your Date field you can specify how the object is being mapped:

  • @Temporal(TIMESTAMP) to timestamp
  • @Temporal(DATE) to date
  • @Temporal(TIME) to time

Also specifying that your column is timestamp with timezone can be stated using annotation @Column(columnDefinition = "TIMESTAMP WITH TIMEZONE").

0
Isaq S On

import java.sql.Timestamp; oR import java.sql.Date; add this import ur pojo .