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
Marco PinderMarco Pinder 

SimpleDateFormat - Parse String to DateTime

Hi all,

 

I am writing a trigger and have a String named 'mappedDueDate' with a value assigned to it, such as:

 

mappedDueDate = '17/11/2011'

 

How can I parse this into a DateTime value so that in my trigger I can create a task with the correct Due Date.

 

e.g.

 

taskActivityDate = mappedDueDate

 

At the moment, I get an error message.

 

I am not a programmer and have received some guidance to understand that I need to parse the string value, but I do not know how.

 

Any help is much appreciated.

 

Thanks,

 

Marco

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Its the other way around - the task activity date is a date field rather than a datetime field, so you need to parse a date out of the string.

 

Change to:

 

Date taskActivityDate=Date.newInstance(Integer.valueOf(mappedDueDate.substring(6,10)), Integer.valueOf(mappedDueDate.substring(4, 5)), Integer.valueOf(mappedDueDate.substring(1,2)));

 

All Answers

bob_buzzardbob_buzzard

You'll need to break the string up into its component parts and create a datetime from it.  However, you don't have a time part of the string - I've assumed you want the time as midnight.

 

Something like:

 

DateTime taskActivityDate=DateTime.newInstance(mappedDueDate.substring(6,10), mappedDueDate.substring(4, 5), mappedDueDate.substring(1,2), 0, 0, 0);

 

Marco PinderMarco Pinder

Hi Bob,

 

Thanks for your reply.

 

I have entered that code and now get the error message:

 

Error: Compile Error: Method does not exist or incorrect signature: DateTime.newInstance(String, String, String, Integer, Integer, Integer) at line 108 column 45

 

Any advice on how to resolve this please?

 

Thanks,

 

Marco

bob_buzzardbob_buzzard

Doh!

 

Change to:

 

DateTime taskActivityDate=DateTime.newInstance(Integer.valueOf(mappedDueDate.substring(6,10)), Integer.valueOf(mappedDueDate.substring(4, 5)), Integer.valueOf(mappedDueDate.substring(1,2)), 0, 0, 0);

 

Marco PinderMarco Pinder

Thanks again Bob, however you are going to dread my response.

 

My error now reads:

 

Error: Compile Error: Illegal assignment from Datetime to Date at line 109 column 17

 

My code on this line is directly under what you have provided and I have written:

 

task.ActivityDate = taskActivityDate;

 

Does the error suggest that the taskActivityDate from your piece of code is a Date rather than Datetime?

 

Do you have any more suggestions? :)

 

Thanks for all your help so far...

 

Marco

bob_buzzardbob_buzzard

Its the other way around - the task activity date is a date field rather than a datetime field, so you need to parse a date out of the string.

 

Change to:

 

Date taskActivityDate=Date.newInstance(Integer.valueOf(mappedDueDate.substring(6,10)), Integer.valueOf(mappedDueDate.substring(4, 5)), Integer.valueOf(mappedDueDate.substring(1,2)));

 

This was selected as the best answer
Marco PinderMarco Pinder

Thanks again Bob.

 

I made the change and now get a compile error:

 

Error: Compile Error: Method does not exist or incorrect signature: Date.newInstance(Integer, Integer, Integer, Integer, Integer, Integer) at line 108 column 41

 

Should I be declaring anything elsewhere in my trigger for this to work?

bob_buzzardbob_buzzard

That error message doesn't match the code that I posted - mine has three integer parameters.  It looks like you've retained the time parsing to.  Try using the exact code from my last post.

Marco PinderMarco Pinder

Thank you very much Bob.

 

I do apologise, I had left the 0,0,0 part in.

 

The code now compiles and I have marked the solution as such.

 

Always impressed with the support of the community here.

 

Thanks again.

 

Marco

bob_buzzardbob_buzzard

Glad to hear you got there!