簡體   English   中英

將DatePicker轉換為java.util.Date

[英]Converting DatePicker to java.util.Date

我在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;
  }
}

java.time

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

它易於管理,並具有自動格式化日期的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM