0

I have datetime field which is coming from api in the below format . I want to store the value in my custom field

Response :
{
time :'2022-06-23T14:00:00+05.30'
timezone:'Asia/Kolkata'
}

I have tried the below in my apex but not able achieve.

String data = '2022-06-23T14:00:00+05.30'; //coming from api
Datetime dt = (DateTime)JSON.deserialize('"' + data.substringbefore('+') + '"', DateTime.class);
System.debug('datatime from api====' + dt);
String strIST= dt.format('yyyy-MM-dd HH:mm:ss', 'IST');
System.debug('IST converion as string====' + strIST);
Datetime dtIST= DateTime.valueof(strIST);
System.debug('IST converion as Datetime====' +dtIST);

Output is : enter image description here

My expected conversion date is 2022-06-23 14:00:00 but i am getting different value. Can anyone please help me to proceed

2
  • Since you have the time with a timezone SID you should be able to take an approach like this, by first removing the "+nn:nn" offset from the date/time string and parsing like you are doing, but then using the code I've provided to do the conversion, using the timezone SID from the response.
    – Phil W
    May 31, 2022 at 11:28
  • It's worth noting also that System.debug always shows Datetime values in UTC, not user time zone.
    – Phil W
    May 31, 2022 at 11:54

1 Answer 1

0

Using the code snippet from my other Q&A answer, you should be able to have:

String data = '2022-06-23T14:00:00+05.30'; // Coming from the API
String zone = 'Asia/Kolkata'; // Coming from the API
Datetime dt = (Datetime) JSON.deserialize('"' + data.substringBefore('+') + '"', Datetime.class);

// Identify the target time zone
TimeZone target = TimeZone.getTimeZone(zone);

// Figure out the offset at this UTC "moment"
Integer offsetAtUTC = target.getOffset(dt);

// Adjust the date/time value to be in the
// target time zone
Datetime targetDatetime = dt.addSeconds(-offsetAtUTC / 1000);

// Now it is in the target time zone, we have
// a new "moment". If the UTC "moment" was
// before a DST transition but the "moment" in
// the target time zone is after that
// transition the calculation will be adrift
Integer offsetAtLocal = target.getOffset(targetDatetime);

if (offsetAtLocal != offsetAtUTC) {
    // There's a drift because of a DST
    // transition. Correct it
    Datetime adjustedDatetime = targetDatetime.addSeconds((offsetAtUTC - offsetAtLocal) / 1000);

    Integer offsetAtAdjusted = target.getOffset(adjustedDatetime);

    // The correction is conditional; if the corrected time is
    // on the same side of the transition as the correction then
    // we need to correct, but we must also correct if leaping
    // forward regardless
    if (offsetAtAdjusted == offsetAtLocal ||
            adjustedDatetime > targetDatetime) {
        targetDatetime = adjustedDatetime;
    }
}

System.debug('Output local: ' + targetDatetime.format('yyyy-MM-dd HH:ss a z', target.getID()));

At the end of this processing, the targetDatetime represents the input date/time in the target time zone (Asia/Kolkata) though actually is a UTC value (which is how Salesforce stores such values). The debug output is:

Output local: 2022-06-23 14:00 PM IST

Note: the output a given user sees depends on their time zone unless you do explicit formatting to a given time zone like I did here. I did this to demonstrate that the UTC (or as Salesforce call it GMT) value that would be stored does correspond with the input date/time in the given input time zone. Also note that simple System.debug logging of a Datetime value actually logs the UTC value.

2
  • Thanks for the response. I am trying to save the value in Datetime field . So i was trying to convert the String to DateTime.valueof(targetDatetime.format('yyyy-MM-dd HH:ss a z', target.getID())) but I am getting runtime error as "System.TypeException: Invalid date/time: 2022-03-23 17:00 PM IST"
    – priyasfdc
    May 31, 2022 at 12:52
  • Just assign the targetDatetime to your Datetime field at the end of the processing. When this field is viewed by a user who is in IST time zone they will see the expected "local" time. If the field is viewed by a user in a different timezone, they will see it in their time zone instead. This is just how Salesforce works.
    – Phil W
    May 31, 2022 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.