private static long periodEnd(long now,long period,LocalDateTime cal){
  if (period < 0)   return Long.MAX_VALUE;
 else   if (period == 0)   return now;
  if (period < 30 * DAY) {
    long localTime=cal.toEpochSecond(ZoneOffset.UTC) * 1000;
    localTime=localTime + (period - (localTime + 4 * DAY) % period);
    return cal.toEpochSecond(ZoneOffset.UTC) * 1000;
  }
  if (period % (30 * DAY) == 0) {
    int months=(int)(period / (30 * DAY));
    long year=cal.getYear();
    int month=cal.getMonthValue();
    cal.withMonth(month + months);
    cal.withDayOfMonth(1);
    return cal.toEpochSecond(ZoneOffset.UTC) * 1000;
  }
  if (period % (365 * DAY) == 0) {
    long years=(period / (365 * DAY));
    long year=cal.getYear();
    long newYear=year + (years - year % years);
    cal.withYear((int)newYear);
    cal.withMonth(1);
    cal.withDayOfMonth(1);
    return cal.toEpochSecond(ZoneOffset.UTC) * 1000;
  }
  return cal.toEpochSecond(ZoneOffset.UTC) * 1000;
}

private static long periodEnd ( long now , long period , LocalDateTime cal ) { if ( period < 0 ) return Long . MAX_VALUE ; else if ( period == 0 ) return now ; if ( period < 30 * DAY ) { long localTime = cal . toEpochSecond ( ZoneOffset . UTC ) * 1000 ; localTime = localTime + ( period - ( localTime + 4 * DAY ) % period ) ; return cal . toEpochSecond ( ZoneOffset . UTC ) * 1000 ; } if ( period % ( 30 * DAY ) == 0 ) { int months = ( int ) ( period / ( 30 * DAY ) ) ; long year = cal . getYear ( ) ; int month = cal . getMonthValue ( ) ; cal . withMonth ( month + months ) ; cal . withDayOfMonth ( 1 ) ; return cal . toEpochSecond ( ZoneOffset . UTC ) * 1000 ; } if ( period % ( 365 * DAY ) == 0 ) { long years = ( period / ( 365 * DAY ) ) ; long year = cal . getYear ( ) ; long newYear = year + ( years - year % years ) ; cal . withYear ( ( int ) newYear ) ; cal . withMonth ( 1 ) ; cal . withDayOfMonth ( 1 ) ; return cal . toEpochSecond ( ZoneOffset . UTC ) * 1000 ; } return cal . toEpochSecond ( ZoneOffset . UTC ) * 1000 ; }
calculates the next period end . the calculation is in local time .
