将DatePicker转换为java.util.Date

时间:2018-11-28 15:52:27

标签: java vaadin11

我在Vaadin 11中有一个DatePicker,我想绑定到Date字段。看起来像这样会起作用:

getBinder().forField(datePicker)
            .withConverter(new LocalDateTimeToDateConverter(ZoneId.systemDefault()))
            .bind(MatchEntry::getMatchDate, MatchEntry::setMatchDate);

但是我遇到以下错误:

no suitable method found for withConverter(com.vaadin.flow.data.converter.LocalDateTimeToDateConverter)
    method com.vaadin.flow.data.binder.Binder.BindingBuilder.<NEWTARGET>withConverter(com.vaadin.flow.data.converter.Converter<java.time.LocalDate,NEWTARGET>) is not applicable
      (cannot infer type-variable(s) NEWTARGET
        (argument mismatch; com.vaadin.flow.data.converter.LocalDateTimeToDateConverter cannot be converted to com.vaadin.flow.data.converter.Converter<java.time.LocalDate,NEWTARGET>))
    method com.vaadin.flow.data.binder.Binder.BindingBuilder.<NEWTARGET>withConverter(com.vaadin.flow.function.SerializableFunction<java.time.LocalDate,NEWTARGET>,com.vaadin.flow.function.SerializableFunction<NEWTARGET,java.time.LocalDate>) is not applicable
      (cannot infer type-variable(s) NEWTARGET
        (actual and formal argument lists differ in length))
    method com.vaadin.flow.data.binder.Binder.BindingBuilder.<NEWTARGET>withConverter(com.vaadin.flow.function.SerializableFunction<java.time.LocalDate,NEWTARGET>,com.vaadin.flow.function.SerializableFunction<NEWTARGET,java.time.LocalDate>,java.lang.String) is not applicable
      (cannot infer type-variable(s) NEWTARGET
        (actual and formal argument lists differ in length))
com/github/javydreamercsw/tournament/manager/ui/views/matchlist/MatchEditorDialog.java:[115,19] invalid method reference
  non-static method getMatchDate() cannot be referenced from a static context

如果需要,这里是MatchEntry类:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "match_entry")
@XmlRootElement
@NamedQueries(
        {
          @NamedQuery(name = "MatchEntry.findAll", query = "SELECT m FROM MatchEntry m"),
          @NamedQuery(name = "MatchEntry.findById",
                  query = "SELECT m FROM MatchEntry m WHERE m.matchEntryPK.id = :id"),
          @NamedQuery(name = "MatchEntry.findByRoundId",
                  query = "SELECT m FROM MatchEntry m WHERE m.matchEntryPK.roundId = :roundId"),
          @NamedQuery(name = "MatchEntry.findByFormatId",
                  query = "SELECT m FROM MatchEntry m WHERE m.matchEntryPK.formatId = :formatId")
        })
public class MatchEntry implements Serializable
{
  private static final long serialVersionUID = 1L;

  @EmbeddedId
  protected MatchEntryPK matchEntryPK;

  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JoinColumns(
          {
            @JoinColumn(name = "FORMAT_ID", referencedColumnName = "ID",
                    insertable = false, updatable = false),
            @JoinColumn(name = "GAME_ID", referencedColumnName = "ID",
                    insertable = false, updatable = false)
          })
  private Format format;

  @ManyToOne(optional = true, fetch = FetchType.LAZY)
  @JoinColumns(
          {
            @JoinColumn(name = "ROUND_ID", referencedColumnName = "ID",
                    insertable = false, updatable = false),
            @JoinColumn(name = "TOURNAMENT_ID", referencedColumnName = "ID",
                    insertable = false, updatable = false)
          })
  private Round round;

  @OneToMany(mappedBy = "matchEntry", fetch = FetchType.LAZY,
          cascade = CascadeType.ALL)
  private List<MatchHasTeam> matchHasTeamList;

  @Basic(optional = false)
  @Column(name = "match_date")
  @Temporal(TemporalType.TIMESTAMP)
  private Date matchDate;

  public MatchEntry()
  {
    setMatchHasTeamList(new ArrayList<>());
  }

  public MatchEntry(MatchEntryPK matchEntryPK)
  {
    this.matchEntryPK = matchEntryPK;
  }

  public MatchEntry(int roundId, int formatId)
  {
    this.matchEntryPK = new MatchEntryPK(roundId, formatId);
  }

  public MatchEntryPK getMatchEntryPK()
  {
    return matchEntryPK;
  }

  public void setMatchEntryPK(MatchEntryPK matchEntryPK)
  {
    this.matchEntryPK = matchEntryPK;
  }

  public Format getFormat()
  {
    return format;
  }

  public void setFormat(Format format)
  {
    this.format = format;
  }

  public Round getRound()
  {
    return round;
  }

  public void setRound(Round round)
  {
    this.round = round;
  }

  public List<MatchHasTeam> getMatchHasTeamList()
  {
    return matchHasTeamList;
  }

  public final void setMatchHasTeamList(List<MatchHasTeam> matchHasTeamList)
  {
    this.matchHasTeamList = matchHasTeamList;
  }

  @Override
  public int hashCode()
  {
    int hash = 0;
    hash += (matchEntryPK != null ? matchEntryPK.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object)
  {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof MatchEntry))
    {
      return false;
    }
    MatchEntry other = (MatchEntry) object;
    return !((this.matchEntryPK == null && other.matchEntryPK != null) 
            || (this.matchEntryPK != null 
            && !this.matchEntryPK.equals(other.matchEntryPK)));
  }

  @Override
  public String toString()
  {
    return "MatchEntry[ matchEntryPK=" 
            + matchEntryPK + " ]";
  }

  public Date getMatchDate()
  {
    return matchDate;
  }

  public void setMatchDate(Date matchDate)
  {
    this.matchDate = matchDate;
  }
}

1 个答案:

答案 0 :(得分:2)

java.time

您可以使用java.time.LocalDate(或java.time.LocalDateTime)代替旧的Date

它更易于管理,并具有自动格式化日期的方式。