I am creating a couple of date objects like such:
var labOrderTime = '20230811'
labOrderTime = DateUtil.convertDate('yyyyMMdd', 'yyyy-MM-dd', labOrderTime)
var start = '20230811125910'
start = DateUtil.convertDate('yyyyMMdd', 'yyyy-MM-dd', start)
I am then trying to compare the two to see if they are on the same day. I am running it to issues though trying to compare date objects that have a timestamp against those that do not.
Something like this:
if(labOrderTime == start ){
return 1
}
if(labOrderTime < start ){
return 2
}
if(labOrderTime > start ){
return 3
}
I am seeing it return 2 because it it treating my labOrderTime object as '2023-08-11 00:00:00'.
I have tried a bunch of different date functions but cannot seem to figure out how to get it to do exactly what I want here. Any assistance would be appreciated.
That seems to be an utility class that is just a wrapper for Java's SimpleDateFormat but adds no value. Also most of the methods return a String, which is what make your comparisons difficult. Note that the patterns for both dates are different. It should had really caused an exception. The mismatch is kind of hidden.
I would recommend to not use this and just use Java more modern java.date.* API. I'm adding an example below. Note how the comparison is very easy once correctly parsed.
import java.time.*;
import java.time.format.*;
public class TestCompareDates {
public static void main(String args[]) throws Exception {
var labOrderTime = "20230811";
var start="20230811125910";
var pattern1="yyyyMMdd";
var pattern2="yyyyMMddhhmmss";
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern(pattern1);
LocalDate dateA = LocalDate.parse(labOrderTime, dateTimeFormatter1);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern(pattern2);
LocalDate dateB = LocalDate.parse(start, dateTimeFormatter2);
System.out.println("dateA= " + dateA);
System.out.println("dateB= " + dateB);
if (dateA.isEqual(dateB)) {
System.out.println("dateA is equal to dateB");
} else if (dateA.isBefore(dateB)) {
System.out.println("dateA comes before dateB");
} else {
System.out.println("dateB comes before dateA");
}
}
}