1

I'm generating a bookings calendar with iCal4j for a Spring Boot app, but all dates come with one month more and two days less, than it should.

For example: (dd/MM/yyyy) for 22/03/2018 in the iCal I get 20/04/2018.

Debugging, I've seen that all dates and values are all right. Therefore I guess the problem starts at the constructor of this class:

net.fortuna.ical4j.model.Date

Date start = new Date(arrival);
Date end = new Date(departure);

This is my method code, in which the dates are of the type LocalDate:

public Calendar getPropertyICal(Integer idproperty) throws SocketException {

    //Initializing an iCal4j calendar
    Calendar iCal = new Calendar();
    iCal.getProperties().add(new ProdId("-//RentalWebs//iCal4j 1.0 by Ben Fortuna//EN"));
    iCal.getProperties().add(Version.VERSION_2_0);
    iCal.getProperties().add(CalScale.GREGORIAN);
    HostInfo host = new SimpleHostInfo("rentalwebs.com");

    List<Booking> upcomingBookings = bookingRepository.getUpcomingBookings(idproperty);

    for(Booking bkng : upcomingBookings){

        java.util.Calendar arrival = java.util.Calendar.getInstance();
        arrival.set(java.util.Calendar.MONTH, bkng.getDatefrom().getMonth().getValue());
        arrival.set(java.util.Calendar.DAY_OF_MONTH, bkng.getDatefrom().getDayOfMonth());
        arrival.set(java.util.Calendar.YEAR, bkng.getDatefrom().getYear());

        LocalDate lastNight = bkng.getDateto().minusDays(1);
        java.util.Calendar departure = java.util.Calendar.getInstance();
        departure.set(java.util.Calendar.MONTH, lastNight.getMonth().getValue());
        departure.set(java.util.Calendar.DAY_OF_MONTH, lastNight.getDayOfMonth());
        departure.set(java.util.Calendar.YEAR, lastNight.getYear());

        Location propertyName = new Location(propertyRepository.getPropertyById(bkng.getIdproperty()).getName());
        String guestName = bkng.getSurname() + ", " + bkng.getName();

        Date start = new Date(arrival);
        Date end = new Date(departure);

        VEvent booking = new VEvent(start, end, guestName);

        UidGenerator ug = new UidGenerator(host, LocalDate.now().toString());//Unique identifier
        booking.getProperties().add(ug.generateUid());
        booking.getProperties().add(propertyName);

        iCal.getComponents().add(booking);

    }

    return iCal;
}

The server is located in Frankfurt, and the constructor of the java.util.Calendar class gets a central European locale, which is all right.

Aleix Alcover
  • 599
  • 3
  • 11
  • 27

1 Answers1

2

Calendar.MONTH is zero-based, so if your booking class uses 1-12 for month you need to subtract one when setting the calendar.

Not sure about the day, but perhaps the fact it's april instead of March may be having an effect on the date..

fortuna
  • 701
  • 5
  • 7
  • The problem with the month was clear and it's solved. I need to take of the last day of the interval, the departure day, which I did on my LocalDate with the lastNight variable. That's why I thought the system was taking two days off, when in fact just takes one off. For me this behaviour is fine, as far as it's consistent. – Aleix Alcover Mar 10 '18 at 17:52