You need to sign in to do that
Don't have an account?
System.TypeException: Invalid date/time
Hi,
I am getting this error message when running this query:
System.TypeException: Invalid date/time: Mon Jan 26 00:00:00 GMT 2009
System.debug(item.Start_Date__c);
myStartdate = datetime.valueOf(item.Start_Date__c);
myExpirationdate = datetime.valueOf(item.Expiration_Date__c);
Job_Posting_History__History[] jphh_items = [SELECT Id, CreatedBy.Id FROM Job_Posting_History__History WHERE (CreatedBy.ProfileId = '00e30000000mHRJ') AND (Field = 'Cross_Posted_to__c') AND (Parent.Id = :jphid) AND (CreatedDate > :myStartdate) AND (CreatedDate < :myExpirationdate) ORDER BY CreatedDate DESC];
Here is piece of the System Log:
20090127223506.172:Class.Check_Package_Inventory_Cross_Posting.checkRHW_Package: line 30, column 21: 2009-01-26 00:00:00
System.TypeException: Invalid date/time: Mon Jan 26 00:00:00 GMT 2009
I have run out of ideas. Please help!
JD
Ah right, converting between Date and DateTime is more annoying than it should be. Ideally, Date should implement getTime() which would simplify a lot of Date v. DateTime issues, but it doesn't.
When converting between Date and DateTime, you want to do something more like this
Date orig = Date.today(); DateTime dtConverted = DateTime.valueOf(orig.format('yyyy-MM-dd HH:mm:ss'));
or
Date orig = Date.today(); DateTime dtConverted = DateTime.newInstance(orig.year(), orig.month(), orig.day());
HTH
Jeremy Kraybill
Austin, TX
All Answers
Is it the query line itself that is hitting the exception? What are the actual types of item.Start_Date__c, item.Expiration_Date__c, myStartDate, and myExpirationDate? I am guessing at least one of them is a string, and it is failing conversion to a DateTime type. Let me know what type the non-DateTimes are, and where they are getting populated from.
Jeremy Kraybill
Austin, TX
Is actually the valueOf methods. I am not working with any string type variable.
Start_Date__c and Expiration_Date__c are just Date type.
myStartdate and myExpirationdate are Datetime
I have to convert them becasue in the query, CreatedDate is Datetime type.
More code follows:
Datetime myStartdate;
Datetime myExpirationdate;
items = [SELECT Id, Start_Date__c, Expiration_Date__c FROM Package_Inventory__c WHERE (Registration__c = :registrationid) AND (Employer_Package_Id__c LIKE '%RHW%') AND (Package_Status__c = 'Not Expired') AND (Unlimited_Package__c = true) ORDER BY CreatedDate ASC];
if (items.size() > 0) {
for (Package_Inventory__c item : items) {
System.debug(item.Start_Date__c);
myStartdate = datetime.valueOf(item.Start_Date__c);
myExpirationdate = datetime.valueOf(item.Expiration_Date__c);
Job_Posting_History__History[] jphh_items = [SELECT Id, CreatedBy.Id FROM Job_Posting_History__History WHERE (CreatedBy.ProfileId = '00e30000000mHRJ') AND (Field = 'Cross_Posted_to__c') AND (Parent.Id = :jphid) AND (CreatedDate > :myStartdate) AND (CreatedDate < :myExpirationdate) ORDER BY CreatedDate DESC];
if (jphh_items.size() == 0) {
// Code.....
}
}
return;
}
Thanks, JD
Ah right, converting between Date and DateTime is more annoying than it should be. Ideally, Date should implement getTime() which would simplify a lot of Date v. DateTime issues, but it doesn't.
When converting between Date and DateTime, you want to do something more like this
Date orig = Date.today(); DateTime dtConverted = DateTime.valueOf(orig.format('yyyy-MM-dd HH:mm:ss'));
or
Date orig = Date.today(); DateTime dtConverted = DateTime.newInstance(orig.year(), orig.month(), orig.day());
HTH
Jeremy Kraybill
Austin, TX
Hi Jeremy,
Thanks for your help. The second suggestion did the trick.
JD