Search code examples
javavaadindatefield

Difference between two dateFields in Vaadin


I'm having some problem with calculate days between two dateFields. I already tried

`

DateField dateStart = new DateField();
DateField dateEnd = new DateField();
DateTime start = new DateTime(dateStart);
DateTime end = new DateTime(dateEnd);

int days = Days.daysBetween(new DateTime(start), new DateTime(end)).getDays();
        `

Here is the error that I get after run this code

 java.lang.IllegalArgumentException: No instant converter found for type: com.vaadin.ui.DateField

I also already tried using ChronoUnit

LocalDate startDate = LocalDate.now().minusDays(1);
LocalDate endDate = LocalDate.now();

long days = Period.between(startDate, endDate).getDays();
assertEquals(1, days);

long days2 = ChronoUnit.DAYS.between(startDate, endDate);
assertEquals(1, days2); 

and also JudoTime

DateTime startDate = new DateTime().minusDays(1);
DateTime endDate = new DateTime();

Days d = Days.daysBetween(startDate, endDate);
int days = d.getDays();

Both code I get NullPointerException error. Is there any way that I can get number of days.


Solution

  • The other answers show how to calculate the difference between two dates in days using Java 8. But your actual problem is how to get the dates from Vaadin DateField (as p.streef and Ramachandran G A pointed out in the comments). Use dateStart.getValue() for that which will return a java.util.Date and can be passed to new DateTime().