0

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.

syira
  • 29
  • 8
  • 1
    I'm not sure what kind of class DateTime is, but creating a new DateTime with a field as input is probably wrong. Why not just use dateStart.getValue() you can convert this Date to ms with getTime() or convert to Calendar or LocalDateTime for more convenient methods – p.streef Jul 27 '16 at 14:07

3 Answers3

1

This is simplest way to find the intermediate date values using java 8.

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

long days = Period.between(startDate, endDate).getDays();
System.out.println(days);
1

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().

Steffen Harbich
  • 2,639
  • 2
  • 37
  • 71
0

The following snippet works. One of the reason for no instant converter error that you see is what time of the day do you want the day to be at. I mentioned start of day here. Interesting read here (How to convert Joda Localdate to Joda DateTime?) .

    LocalDateTime startDateTime = LocalDate.now().atStartOfDay().minusDays(1);

    LocalDateTime endDateTime = LocalDate.now().atStartOfDay();

    long days = Period.between(startDateTime.toLocalDate(), endDateTime.toLocalDate()).getDays();
    System.out.println("The difference in dates is " + days);

At the output : The difference in dates is 1

Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24
  • Can I change DateField to LocalDate? – syira Jul 28 '16 at 03:45
  • 1
    Assuming date field is from this , https://vaadin.com/api/com/vaadin/ui/DateField.html getValue() on the superClass gives a Date. So yourVaadinDateTime.getValue() returns a Date. Date in = new Date(); LocalDateTime startDateTime = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault()).minusDays(1); – Ramachandran.A.G Jul 28 '16 at 04:40