Try this ;)
You have to pass function parameter value in String format then deserialize them;
@AuraEnabled
public static void insertObjects(String objects) {
List<Object__c> objList = (List<Object__c>) Json.deserialize(objects, List<Object__c>.class);
System.debug(objList);
}
So, it sounds like you are trying to do the following:
- Retrieve a
String
that represents time in PST timezone
- Convert that time to PST DateTime
- Find the GMT representation of the PST
DateTime
In order to convert the String
that represents time in PST, you need to create a DateTime
instance in GMT and then use the format()
method to convert to PST.
Datetime pstDateTime = Datetime.valueOf(Datetime.newInstanceGMT(System.today(), <Time>).format('yyyy-MM-dd HH:mm:ss', 'PST'));
Once you have the pstDateTime
, you can then create another instance of DateTime
for GMT by manually constructing this instance using the pstDateTime
attributes.
Datetime gmtDateTime = Datetime.newInstanceGMT(pstDateTime.year(), pstDateTime.month(), pstDateTime.day(), pstDateTime.hour(), pstDateTime.minute(), pstDateTime.second());
Using the code you have provided in your question and my code to resolve your issue, the following should work.
String timeStr = '15:00';
String defaultTime = '00:00';
String[] ct =
(timeStr != null) ?
timeStr.split(':') :
defaultTime.split(':');
Time t = Time.newInstance(Integer.valueOf(ct[0]), Integer.valueOf(ct[1]), 0, 0);
Datetime pstDateTime = Datetime.valueOf(Datetime.newInstanceGMT(System.today(), <Time>).format('yyyy-MM-dd HH:mm:ss', 'PST'));
Datetime gmtDateTime = Datetime.newInstanceGMT(pstDateTime.year(), pstDateTime.month(), pstDateTime.day(), pstDateTime.hour(), pstDateTime.minute(), pstDateTime.second());
Best Answer
Datetime objects have this date method:
so your code can use it like this:
(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.