Domanda

I want to validate date in my DateBox. There is how I do it:

private DateBox addContDateCreateDateBox = new DateBox();
private DateTimeFormat ft_big = DateTimeFormat.getFormat("dd.MM.yyyy");
addContDateCreateDateBox.setFormat(new DateBox.DefaultFormat(ft_big));
addContDateCreateDateBox.setValue(new Date());
Boolean fl = true;
if (addContDateCreateDateBox.getValue() != null) {
  try {                     
   ft_big.parseStrict(addContDateCreateDateBox.getValue().toString());
  } catch (IllegalArgumentException ex) {
   fl = false;              
  }
} else  fl = false;         

But even if I put correct date in DateBox I receive fl==false. I don't know why but this addContDateCreateDateBox.getValue().toString() against '02.08.2013' return 'Fri Aug 02 00:00:00 EEST 2013'.

Please, help.

È stato utile?

Soluzione

DateTimeFormat.parseStrict()

expects a String and

addContDateCreateDateBox.getValue().toString() 

returns 'Fri Aug 02 00:00:00 EEST 2013' which is not parseable according to the format you specified ("dd.MM.yyyy") in

private DateTimeFormat ft_big = DateTimeFormat.getFormat("dd.MM.yyyy");

Instead use

ft_big.format(addContDateCreateDateBox.getValue()); 

to format the date returned by

addContDateCreateDateBox.getValue() 

After the date is properly formatted you can use the parseStrict() method to enforce the format is respected.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top