We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
That's true, but the above code accounts for that, albeit weirdly.
Calendar.MONDAY, ... constants have fixed specific values, starting with SUNDAY = 1 (I guess Java was first developed in the US). So the days array is shifted by 1 and doesn't reflect those constants... or does it?
... notice that @devesh_vyas actually subtracted 1 from the day as well in the ctor call, which leads to the above code calculating the day before what's given in the input. This can be easily seen:
System.out.println(cal.getTime());
So it uses "yesterday"'s DAY_OF_WEEK to index an array that has been shifted by 1 leading to the wanted result, confusing, but mostly correct.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Date and Time
You are viewing a single comment's thread. Return to all comments →
That's true, but the above code accounts for that, albeit weirdly.
Calendar.MONDAY
, ... constants have fixed specific values, starting withSUNDAY = 1
(I guess Java was first developed in the US). So thedays
array is shifted by1
and doesn't reflect those constants... or does it?... notice that @devesh_vyas actually subtracted
1
from the day as well in the ctor call, which leads to the above code calculating the day before what's given in the input. This can be easily seen:So it uses "yesterday"'s
DAY_OF_WEEK
to index an array that has been shifted by1
leading to the wanted result, confusing, but mostly correct.