Hi,

I have the following test that from my understanding should pass. Is there something I'm missing or is this a bug in Calendar?

    Calendar inputC = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US);

    // Sunday
    inputC.set(2014, Calendar.JUNE, 22, 0, 0, 0);
    inputC.set(Calendar.MILLISECOND, 0);

    // Code tested. Given a date returns back the date with it's day being first day of week and resets time.
    Calendar dc = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.US);
    dc.set(inputC.get(Calendar.YEAR), inputC.get(Calendar.MONTH), inputC.get(Calendar.DAY_OF_MONTH), 0, 0 , 0);
    dc.set(Calendar.MILLISECOND, 0);
    // dc.getTimeInMillis();
    // dc.set(Calendar.WEEK_OF_YEAR, inputC.get(Calendar.WEEK_OF_YEAR));
    dc.set(Calendar.DAY_OF_WEEK, dc.getFirstDayOfWeek());

    Date output = dc.getTime();

    Calendar expectedSundayC = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US);
    expectedSundayC.set(2014, Calendar.JUNE, 22, 0, 0, 0);
    expectedSundayC.set(Calendar.MILLISECOND, 0);

    assertEquals(output, expectedSundayC.getTime());


 

Output: 2014-06-15T00:00:00Z

Expected: 2014-06-22T00:00:00Z

 

The above test fails unless I uncomment line 11 or 12. Why is that dc.getTimeInMillis() affects the output?

Line 12 seems redundant because line 9 and 10 sets all necessary fields for complete datetime.

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners