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
SBKSBK 

DateTime - System.TypeException: Invalid date/time: 2010-02-25T09:50:30Z

I need the currentTime as a DateTime object to use it my SOQL like :

 

m.End_Time__c < :currentTime

 

where End_Time__c is of type "DateTime".

 

Here is the relevant piece of code

 

 

public Datetime getCurrentDateTime() {

return DateTime.valueOf(System.now().format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\''));

}

 

 

testmethod static void test() {

ApexPages.StandardController controller = new ApexPages.StandardController(new My_Custom_Object__c());

MyController mc = new MyController(controller);

System.debug('>>>>>>>>>>>>>>>> ' + mc.getCurrentDateTime());

 

 When I run the test, I get the following exception :

 

 System.TypeException: Invalid date/time: 2010-02-25T09:50:30Z in the line highlighted red

 

 What is wrong with the date format? Looks ok to me...

 

  Thanks a bunch.. 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

In apex, you can just bind it directly, e.g.

 

DateTime dt = System.now();

[select someField__c from somewhere__c where myField__c > :dt]; 

All Answers

DevAngelDevAngel
Why not just return System.now?
SuperfellSuperfell
System.now() already returns a DateTime, why go through the hops of formatting it and re-parsing it ?
SBKSBK

Thanks for your replies.

 

How will I use it in my SOQL?

 

 

 

Select m.Start_Time__c From My_Obj__c mWHERE m.Start_Time__c > 2010-02-25 18:34:09

 

System.now() returns "2010-02-25 18:34:09" and SOQL doesn't like it. It complains it is a MALFORMED QUERY.

 

Start_Time__c is of the type 'DateTime'


Thanks 

 

 

 

SBKSBK

 

 

Here is how data is stored:

 

 

Start_Time__c

 

2010-02-25T02:01:00.000Z

 

 

SuperfellSuperfell

In apex, you can just bind it directly, e.g.

 

DateTime dt = System.now();

[select someField__c from somewhere__c where myField__c > :dt]; 

This was selected as the best answer