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
kk909kk909 

Input Date value

Hi !

 

Can I know how to assign date (for example: some random date-- 24/01/1963) into a variable, in Apex.

 

Do we need to follow any date formats for this? 

 

Thanks.

Mohith Kumar ShrivastavaMohith Kumar Shrivastava
date x=date.newinstance(1963, 01,24);
system.debug('%%%%%%'+x);

 Use this .

 

Or if you are trying to use in test class use ..

Date x=System.today()+10;

Date Y=System.today()-5;

 

kk909kk909

Thanks Mohit.

 

My requirement was to assign sample date from test class and print in business class.

 

I've assigned a sample date to some date variable and sent the variable from test class and printed the date in Business class.

 

I could see the output. But only in year,month,day format. 

 

Also please let me know if I can print the date in other formats..

 

Thanks once again..

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

How do you like the format to be .

 

Is it on visualforce Page you want the format?

kk909kk909

No, Its not on the Visual Force page.

 

I am printing the outputs to log file using system.debug(). So, I want the outputs on the log file not on visual force page.

Mohith Kumar ShrivastavaMohith Kumar Shrivastava
datetime x=datetime.newinstance(1963, 01,24);
system.debug('%%%%%%'+x.format('yyyy-MM-dd'));
system.debug('&&&&&&'+x.format('yyyy/MM/dd'));

 You can use Date time and then this has a method to format .See the above code snippet

kk909kk909

By format, I mean printing date in dd-mm-yyyy format or mm-dd-yyyy format etc..

 

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

Yes same .You can modify the above code and format that accordingly.Please find more updates

 

 

datetime x=datetime.newinstance(1963, 01,24);
system.debug('%%%%%%'+x.format('yyyy-MM-dd'));
system.debug('&&&&&&'+x.format('yyyy/MM/dd'));
system.debug('#####'+x.format('dd-MM-yyyy'));
System.debug('$$$$$$$'+x.format('MM-dd-yyyy'));

 

kk909kk909

Hi ! Mohit.

 

Sorry for repeating the question. I will paste the code which is showing error. Please rectify the statement.

 

dob is variable which has date assigned from test method, which is 1960,01,20

 

If I print dob I get date without any errors. But only in yyyy-mm-dd format.

 

I've used your format function but I get the error.

"Error: Compile Error: Method does not exist or incorrect signature: [Date].format(String) at line 31 column 49"

 

Statement : System.debug('Employees Date of Birth:'+dob.format('yyyy-mm-dd'));

 

Please tell me where I've went wrong..

 

Thanks.

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

I have used DateTime as a Variable .Use DateTime as a Variable you will not have any exception.

 

If you want to use Date as Variable then you cannot use Date.format .Please see the code properly in my earlier post where i formatted .Thanks.

 

Also for logging purpose you may convert Date to DateTime .Use Datetime.valueOf(Date).Let me know if you need further help from me .