[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

comparing two strings

I'm writing some code that is in the salesforce apex language that is a derivative of the Java language, which is why I'm posting in this zone.

I'm trying to compare two strings.  The strngs are time intervals.

For example,

8:00 AM
8:05 AM
8:10 AM
8:15 AM
etc.

So, I'm looping over the time interval strings and I want to compare against another time interval string to see if the time is less or greater.

I was trying the following (see code below), but it doesn't work.

So, if my time slot value is "10:15 AM" and my start time value is 10:00 AM and my end time value is 11:30 AM

I would want to enter my branch of code because time slot value is greater than the start time and less than the end time, but It is not working correctly?

How can I achieve this?

Thanks for any help.
if (s.Time_Slot__c.CompareTo(ev.Screening_Close_Start_Time__c) > 0 && s.Time_Slot__c.CompareTo(ev.Screening_Close_End_Time__c) < 0)

Open in new window

0
-Dman100-
Asked:
-Dman100-
  • 5
  • 2
2 Solutions
 
for_yanCommented:
what is s.Time_Slot__c
if that is a string then method id compareTo() with lower case not CompareTo
0
 
for_yanCommented:
if (s.Time_Slot__c.compareTo(ev.Screening_Close_Start_Time__c) > 0 && s.Time_Slot__c.compareTo(ev.Screening_Close_End_Time__c) < 0)

Open in new window

0
 
for_yanCommented:
In general if you want to compare the time represented by strings that is not right way to go becauese
01:05 PM will turn out to be less that 08:05 AM
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
for_yanCommented:

You need to do siomething like this:

SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm a");
(sdf.parse("OCT-01-2011 " + s.Time_Slot_c,new ParsePosition(0))).compareTo(sdf.parse("OCT-01-2011 " + ev.Screening_Close_Start_Time_c),new ParsePosition(0))) > 0 && sdf.parse("OCT-01-2011 " + s.Time_Slot_c,new ParsePosition(0))).compareTo(sdf.parse("OCT-01-2011 " + ev.Screening_Close_End_Time_c),new ParsePosition(0)) <0) 

Open in new window

0
 
for_yanCommented:
Mind that the above is not tested - number of parentheses should be checked and adjusted
0
 
CEHJCommented:
You need the following, where 'referenceDate' is the Date derived from the time string to which you're comparing:
DateFormat df = new SimpleDateFormat("hh:mm a");
Date referenceDate = df.parse(referenceTimeString);
int result = df.parse(timeStringToTest).compareTo(referenceDate);

Open in new window

0
 
CEHJCommented:
The accepted answer will *not* parse the string you gave in your first example btw
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now