2

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?

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

1 Answer 1

3

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(
        Date.parse(startDate),
        Time.newInstance(
            Integer.valueOf(hourComponents[0]),
            Integer.valueOf(hourComponents[1]),
            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);
1
  • Thanks for help! But how i must assign pased value to the ev.ActivitydateTime?
    – Viktor
    Commented Sep 23, 2016 at 11:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .