简体   繁体   中英

I can't set properly the dates for an event with iCal4j

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM