There are three data types associated with dates and times. The Time data type stores times (hours, minutes, second and milliseconds). The Date data type stores dates (year, month and day). The Datetime data type stores both dates and times.
Each of these classes has a newInstance method with which you can construct particular date and time values. For example, execute the following:
Date myDate = Date.newinstance(1960, 2, 17); Time myTime = Time.newInstance(18, 30, 2, 20); System.debug(myDate); System.debug(myTime);
The Date data type does hold a time, even though it's set to 0 by default.
You can also create dates and times from the current clock:
Datetime myDateTime = Datetime.now(); Date today = Date.today();
The date and time classes also have instance methods for converting from one format to another. For example:
Time t = DateTime.now().time();
Finally, you can also manipulate and interrogate the values by using a range of instance methods. For example, Datetime has the addHours, addMinutes,dayOfYear, timeGMT methods and many others. Execute the following:
Date myToday = Date.today(); Date myNext30 = myToday.addDays(30); System.debug('myToday = ' + myToday); System.debug('myNext30= ' + myNext30);
You'll get something like this as the output.