title: Date Conversion in Struts2
body: <p>I am wondering how to convert a <code>String</code> to <code>Date</code> in Struts2. I have a simple form in which user provide the date in this format <code>"yyyy-MM-dd"</code>. Upon submit the Sturts2 maps form that to the bean. I got error in date conversion. I Google it a lot and every where it is stated that we have to use custom type converter for this. I don't want to write a custom type converter for date conversion. I think there should be an easy mechanism in Struts2 for data conversion because data conversion is very common functionality.</p>
  
  <p><strong>JSP</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;s:form action="AddDomain"&gt;
      &lt;s:push value="idp"&gt;
          &lt;s:textfield name="domainName"         label="Domain Name" /&gt;
          &lt;s:textfield name="url"                label="Domain URL" /&gt;
          &lt;s:textfield name="noOfLicense"        label="License Purchased" /&gt;
  
          &lt;s:textfield name="licenseExpireDate"  label="License Expire Date" 
                                                 title="YYYY-MM-DD like 2013-01-21" /&gt;
  
          &lt;s:textfield name="userActiveDuration" label="Active User Duration"
                                                 title="please mention in days" /&gt;
  
          &lt;s:textarea  name="notes"              label="Note" cols="30" rows="5" &gt;&lt;/s:textarea&gt;
  
          &lt;s:submit value="Add" /&gt;
      &lt;/s:push&gt;
  &lt;/s:form&gt;
  </code></pre>
  
  <p>This is the JSP Where user enter the input.</p>
  
  <p><strong>Model Class</strong></p>
  
  <pre><code>@Entity
  @Table(name = "Domain")
  public class IdentityProvider implements Serializable {
  
      @Id
      @Basic(optional = false)
      private String url;
      private String domainName;
      private int noOfLicense;
      private int userActiveDuration;
      private int activeUsers;
      private Date licenseExpireDate;
      private String notes;
  
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private String domainIdCode;
  
      public IdentityProvider(String name, String url, int nol, int time,Date d,String notes) {
          this.setDomainName(name);
          this.setUrl(url);
          this.setNoOfLicense(nol);
          this.setUserActiveDuration(time);
          this.setLicenseExpireDate(d);
          this.setNotes(notes);
      }
  
      public IdentityProvider() {
  
      }
  
      // Getter Setter
  }
  </code></pre>
  
  <p><strong>Action Class</strong></p>
  
  <pre><code>public class DomainManagementAction extends ActionSupport 
                                   implements ModelDriven&lt;IdentityProvider&gt; {
  
      private IdentityProvider idp = new IdentityProvider();
  
      public IdentityProvider getIdp() {
          return idp;
      }
  
      public void setIdp(IdentityProvider idp) {
          this.idp = idp;
      }
  
      public String saveDomain() {
          IDPBroker broker = new IDPBroker();
          broker.saveDomain(idp);
          return ActionSupport.SUCCESS;
      }
  
  
      @Override
      public IdentityProvider getModel() {
          // TODO Auto-generated method stub
          return idp;
      }
  
  }
  </code></pre>