On Thu, 29 Feb 2024 09:34:05 GMT, Prasanta Sadhukhan <psadhuk...@openjdk.org> 
wrote:

> Test failed with the exception java.lang.IllegalArgumentException: (start <= 
> value <= end) with no history of failing till date.
> Investigation shows SpinnerDateModel constructor condition is not satisfied 
> because
> It seems it's a leap year problem that is being manifested..
> 
> As per test,
> initDate Thu Feb 29 13:49:08 IST 2024 [ obtained by calendar.getTime();]
> earliestDate Tue Feb 28 13:49:08 IST 2023 [obtained by 
> calendar.add(Calendar.YEAR, -1);]
> latestDate Wed Feb 28 13:49:08 IST 2024 [obtained by 
> calendar.add(Calendar.YEAR, 1);]
> 
> Now, as per SpinnerDateModel constructor
> earliestDate <= initDate condition is satisfied
> but
> latestDate >= initDate is not
> so [start <= value <= 
> end](https://github.com/openjdk/jdk/blob/998d0baab0fd051c38d9fd6021628eb863b80554/src/java.desktop/share/classes/javax/swing/SpinnerDateModel.java#L185)
>  condition fails
> 
> Not sure it anything can be done in Calendar class for this but fix is done 
> in test taking leapyear into account so that we add a day if it's a leapyear 
> when we consecutively do Calendar.add(YEAR, -1) and Calendar.add(YEAR,1) on 
> leapyear date of 29Feb

The problem comes down to this
% cat LeapDayBug.java 
import java.util.Calendar;

public class LeapDayBug {
   public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2024, 1, 29);
        System.out.println(calendar.getTime());
        calendar.add(Calendar.YEAR, -1);
        calendar.add(Calendar.YEAR, 1);
        System.out.println(calendar.getTime());
   }
}

% java LeapDayBug.java 
Thu Feb 29 12:27:46 PST 2024
Wed Feb 28 12:27:46 PST 2024

So I'm not sure what the Calendar class could do about this since both
the -1 and the + 1 are correct considered standalone, but it just is not round 
trippable.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/18054#issuecomment-1971906144

Reply via email to