function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShravankumarShravankumar 

How can i insert date through Apex code?


Hi,

    I want to insert a date through apex code,how can i mention the date data type in apex.. 


Training__c obj=new Training__C();
obj.name='T50';
insert obj;
batch__C b=new batch__C();
b.training__c = obj.id;
b.End_Date__c = 
b.Start_Date__c = 

insert b; 

Navatar_DbSupNavatar_DbSup

Hi

 

Try the below code snippet as reference:

 

Training__c obj=new Training__C();

obj.name='T50';

insert obj;

batch__C b=new batch__C();

b.training__c = obj.id;

b.End_Date__c = system.today();

b.Start_Date__c = system.today();

 

insert b;

 

or

 

date mydate = date.parse('05/11/2012');

 

Training__c obj=new Training__C();

obj.name='T50';

insert obj;

batch__C b=new batch__C();

b.training__c = obj.id;

b.End_Date__c = mydate;

b.Start_Date__c = mydate;

 

insert b;

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

ShravankumarShravankumar

 

It's getting error, i want to insert the below values.Could please help me

 

End_Date__c = 06/07/2012
Start_Date__c = 07/01/2012

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

 

Training__c obj=new Training__C();

obj.name='T50';

insert obj;

 

batch__C b=new batch__C();

 

b.training__c = obj.id;

 

b.End_Date__c = date.parse('06/07/2012');

b.Start_Date__c = date.parse('07/01/2012');

 insert b;

Gunners_23Gunners_23

Pls try the below code snippet

 

Training__c obj=new Training__C();

obj.name='T50';

insert obj;

 

batch__C b=new batch__C();

 
Date myDate = date.newInstance(2012,05,22);

b.training__c = obj.id;

 

b.End_Date__c = myDate;

b.Start_Date__c = myDate;

 insert b;

hasvitha chowdaryhasvitha chowdary
Date myDate = date.newInstance(2012,05,22);
yes this code is working
smriti sharan19smriti sharan19
We can understand how to write date in apex with help of example. Suppose you want to insert account and you have custom field called SLAExpirationDate__c  which is of date data type . 

To run the below code 
1. Go to Setup 
2. Open Developer Console 
3. Select Debug
4. Open Anonymous Window
5. Execute following code and observe the result

Case 1 
Now if you use '' for date 
 Account a = new Account();
        a.Name = 'Gyan';
        a.Active__c = 'No';
        a.SLAExpirationDate__c = '4/17/2020';
        insert a;
        system.debug('account inserted :'+ a);
    }

Result 
Illegal assignment from String to Date. Error is coming because it is of date type and you are  writing in string format 

Case 2 
 Account a = new Account();
        a.Name = 'Gyan';
        a.Active__c = 'No';
        a.SLAExpirationDate__c = '4/17/2020';
        insert a;
        system.debug('account inserted :'+ a);
    }
Illegal assignment from String to Integer. Error is coming because it is of date type and you are  writing in integer format 


Case 3
   Account a = new Account();
        a.Name = 'Gyan';
        a.Active__c = 'No';
        a.SLAExpirationDate__c = date.parse('4/17/2020');
        insert a;
        system.debug('account inserted :'+ a);
    }

Result: Code Runs Successfully

You need to use date.parse to execute the code

I hope it is clear now. If you like my answer please mark it as best answer.

To understand concepts more clearly go to (http://www.sfdcamplified.com)
 
Swapnil DikshitSwapnil Dikshit
Account a = new Account();
        a.Name = 'Gyan';
        a.Active__c = 'No';
        a.SLAExpirationDate__c = date.parse('4/17/2020');
        insert a;
        system.debug('account inserted :'+ a);
    }
In this we pass Format of date dd/mm/yyyy

date.parse('4/17/2020'); is giving error
System.TypeException: Invalid date: 04/17/2022
Account a = new Account();
        a.Name = 'Gyan';
        a.Active__c = 'No';
        a.SLAExpirationDate__c = date.parse('17/12/2022');
        insert a;
        system.debug('account inserted :'+ a);
   Result: Code Runs Successfully