I have two strings that populated with values: Startdate and StartHour. The StartDate string is populated with the date and the StartHour populated with the time in format hh:mm.

When I populate the ActivityDateTime field, it gives me a value in the AM.

ev.ActivityDateTime = DateTime.valueof(Date.parse(StartDate)+'T'+startHour+':00');

These are the values I'm using to populate the ActivityDateTime:

  • startHour: 14:00

  • startDate: 9/23/2016

So when the user picks the time 14:00 or higher in the ActivityDateTime field, it shows as 12:00 AM.

How can I define AM or PM in the Apex Class or make automatically defined?

  • What if you try valueOfGmt? Alternatively, you could do newInstanceGmt(Date, Time) and split startHour on ':' to create a Time instance. – Adrian Larson Sep 23 '16 at 10:17
  • if I use DateTime.valueofGmt(Date.parse(StartDate)+'T'+startHour+':00'); it always sets the time to 3 AM. – Viktor Sep 23 '16 at 10:26
  • 1
    Can you edit your post to include explicit sample values for startDate and startHour? – Adrian Larson Sep 23 '16 at 10:43
  • @AdrianLarson edited – Viktor Sep 23 '16 at 10:54

I find the Datetime.newInstanceGmt(Date, Time) method to be more consistent. This approach seems to work:

public static Datetime parse(String startDate, String startHour)
    List<String> hourComponents = startHour.split(':');
    return Datetime.newInstanceGmt(
            0, 0

You would then just do:

ev.ActivityDateTime = parse(startDate, startHour);

Or if it's in a separate class:

ev.ActivityDateTime = SomeClass.parse(startDate, startHour);
|improve this answer|||||
  • Thanks for help! But how i must assign pased value to the ev.ActivitydateTime? – Viktor Sep 23 '16 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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