One should be very careful with converting an UTCDateTime value to a Date and/or Time value or vice versa.
The problem is that values of type UTCDateTime are stored in the database in UTC time zone and in code they are accessible in UTC time zone too. But in the UI UTCDateTime values are displayed in user preferred time zone in most cases. Date and Time values are not related to any time zone, so in fact both stored and displayed values are equal.
Because of that each field or variable of type Date or Time should be strictly associated with some fixed time zone (in most cases it makes sense to associate it with user preferred time zone because it’s what user see in the UI) and explicit conversion should always be performed during assignments or comparisons. There are special methods in DateTimeUtil class to switch time zones.
Here is an example of correct conversion between UTCDateTime value and Date and Time value which is associated with user preferred time zone:
public static void testDateTimeConversion()
dateTime = DateTimeUtil::utcNow();
dateInUserTimeZone = DateTimeUtil::date(DateTimeUtil::applyTimeZoneOffset(dateTime, DateTimeUtil::getUserPreferredTimeZone()));
timeInUserTimeZone = DateTimeUtil::time(DateTimeUtil::applyTimeZoneOffset(dateTime, DateTimeUtil::getUserPreferredTimeZone()));
dateTime = DateTimeUtil::newDateTime(dateInUserTimeZone, timeInUserTimeZone, DateTimeUtil::getUserPreferredTimeZone());