Datetime objects have this date method:
date()
Returns the Date component of a Datetime in the local time zone of the
context user.
so your code can use it like this:
Datetime dt = (Datetime) resultSet.get('LastModifiedDate');
sf.lastModifiedDate = dt.date();
(Generally you would need a null check in the code but as LastModifiedDate
always has a value here you don't.)
So this gets you the correct Date
object value. But an answer is needed to mast0r's question to cover how to get the final presentation right.
deserializing your datetime (both utc & ISO) in datetime.class should solve the issue.
JSON.desrialize('datetime' , Datetime.class);
i have tested both format in anynomous block and it is working fine.
Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));
Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));
Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime
OP Example modified
String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';
Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);
System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));
OP mentioned that day and month i always showing 01.
2 points:
- datetime string will be needed in double quotes ( my mistake i disregarded that previously)
- deserialize method is only working with proper iso format after using double hence we can check if date is in iso format then deserialize it otherwise just get value of datetime using datetime.valueof
String strDate1 = '2017-12-17T11:50:50.000Z';
String strDate2 = '2016-11-16 11:50:55';
Datetime date1 = (DateTime)(strDate1.contains('T')?(Datetime)JSON.deserialize('"' + strDate1 + '"' ,Datetime.class) : DateTime.valueof(strDate1));
Datetime date2 = (Datetime)(strDate2.contains('T')?(Datetime)JSON.deserialize('"' + strDate2 + '"' ,Datetime.class) : DateTime.valueof(strDate2));
System.debug('date 1 = ' + date1);
System.debug('date 2 = ' + date2);
System.debug('date11 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 21 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));
======================================================================
Updated Solution
String strDate1 = '2020-01-01T11:50:50.000Z';
Datetime strDate1 = (strDate1==null)?null:Datetime.valueOf(strDate1.replace('T',' '));
Datetime date1 = Date.newInstance(strDate1.yearGmt(),strDate1.monthGmt(),strDate1.dayGmt());
======================================================================
And, this solution works perfectly irrespective of whether the date comes in ISO8601 format or UTC format.
Best Answer
DateTime.newInstance()
does not accept a String as its parameter. You will need to parse the incoming string to create aDateTime
value. There are (at least) three approaches to this:DateTime.newInstance()
yourself.DateTime.parse()
method, which does accept a string. However, it's important to note that this methodFor that reason, if you cannot ensure this code runs in a user context with a specific locale that matches the date format of your incoming data, you should not use it, and its fragility suggests it's a poor solution overall.
DateTime.valueOf()
method, or deserialize JSON into a class with aDateTime
member variable. In both cases, the incoming Date[time] string should be in ISO format:yyyy-MM-dd HH:mm:ss
.