Be Good & Do Good!

Salesforce : Apex : Covert DateTime to Date

I look through the documentation and didn’t see a system method that converts a date time field to a date. There is one for formula fields but is there one for Apex?

Solution : Just create a date newInstance() and pass in just the year,month,day from the dateTime object.

Datetime dateTimetemp = System.now();

Date dateTemp = Date.newInstance(dateTimetemp.year(),dateTimetemp.month(),dateTimetemp.day());

5 Comments

  1. Manish

    Thanks a trillion mate for the solution !!

  2. Priya

    Hi,
    It’s still giving time stamp but with all zeros. 00:00:00 can you please tell me how can I get rid of the time?

  3. Priya

    Neer mind got the solution
    http://paulforce.wordpress.com/2009/08/27/formatting-time-in-apex/

  4. Balu

    Thanks for giving good solution bro.

  5. Arjun Singh

    Priya, Can you tell what is the solution here

Leave a Reply

Your email address will not be published. Required fields are marked *