I've just spent two days banging my head against the Salesforce wall, and the problem came down to this:
Date wkStart = Date.newInstance(2019,10,12); Datetime wkStart1 = wkStart; Datetime wkStart2 = Datetime.newInstance(wkStart,Time.newInstance(0,0,0,0));
wkStart2 are not equal, at least in my non-GMT timezone. It appears that the implicit cast sets it to midnight GMT, while the explicit newInstance sets it to midnight my time zone. (An explicit cast behaves the same as the implicit cast.)
Is there a logic behind that? Why would an implicit midnight be interpreted differently than an explicit midnight? Further, is that documented somewhere? I've searched all I can think of to search on in the Apex manual, and haven't been able to find anything.