ical4j / ical4j

A Java library for parsing and building iCalendar data models
https://www.ical4j.org
BSD 3-Clause "New" or "Revised" License
767 stars 201 forks source link

Time zone lost #335

Open RenHong-HC opened 5 years ago

RenHong-HC commented 5 years ago

Describe the bug I generated a weekly reminder rule, now I want to get the next reminder time through the rule, but I can’t get it.

To Reproduce Steps to reproduce the behavior:

  1. I did a time zone conversion,My start timestamp is: 1561078800000, my local time zone is: UTC+8, the target time zone ID is: America/Noronha, now I convert the timestamp above to the target time zone, the time is: 20190620T230000, then I Through this time as a seed, to get the next execution time, the return time is empty,code show as below

      `TimeZoneRegistry timeZoneRegistry = TimeZoneRegistryFactory.getInstance().createRegistry();
        TimeZone timeZone = timeZoneRegistry.getTimeZone("America/Noronha");
        DateTime seed = new DateTime(1561078800000L);
        seed.setTimeZone(timeZone);
        Recur recur = new Recur("FREQ=WEEKLY;INTERVAL=1");
        Date date = recur.getNextDate(seed, new DateTime(System.currentTimeMillis()));
        System.out.println("-----------------" + date);`
  2. I tracked the code and found that the target time zone was lost.The problem source is as follows,There was a problem here in retVal.add(Dates.getInstance(cal.getTime(), type)),cal.getTime() returns a local timestamp, and there is no time zone set in Dates.getInstance, so the time I get here is my own time zone, not the time of my target time zone.

    ` private class WeeklyExpansionFilter implements Function<Date, List> {

     private final Value type;
    
     public WeeklyExpansionFilter(Value type) {
         this.type = type;
     }
    
     @Override
     public List<Date> apply(Date date) {
         List<Date> retVal = new ArrayList<>();
         final Calendar cal = getCalendarInstance(date, true);
         final int weekNo = cal.get(Calendar.WEEK_OF_YEAR);
         // construct a list of possible week days..
         cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
         while (cal.get(Calendar.WEEK_OF_YEAR) == weekNo) {
             if (!dayList.stream().map(weekDay -> WeekDay.getCalendarDay(weekDay))
                     .filter(calDay -> cal.get(Calendar.DAY_OF_WEEK) == calDay)
                     .collect(Collectors.toList()).isEmpty()) {
                 retVal.add(Dates.getInstance(cal.getTime(), type));
             }
             cal.add(Calendar.DAY_OF_WEEK, 1);
         }
         return retVal;
     }
     } ` 
RenHong-HC commented 5 years ago

I found that the source code has been fixed in February, and the 3.0.6 version was released in January, so it still exists.

benfortuna commented 5 years ago

Apologies I forgot to make a new release with the changes. Have opened a PR now and will create release shortly: https://github.com/ical4j/ical4j/pull/336

RenHong-HC commented 5 years ago

Apologies I forgot to make a new release with the changes. Have opened a PR now and will create release shortly: #336 Thank you very much, I have seen the new version you posted

edwhiting commented 4 years ago

This issue looks fixed to me. @RenHong-HC are you happy to close this ticket?