0

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
D. Fowler
  • 5
  • 2
  • 6
    What is `DateUtil`? – AKX Aug 24 '23 at 18:08
  • @AKX sorry I should have clarified. I am doing this all in Mirth Connect. It is a built in class. http://javadocs.mirthcorp.com/connect/3.0.0/user-api/com/mirth/connect/server/userutil/DateUtil.html – D. Fowler Aug 24 '23 at 18:11
  • 2
    JavaScript and Java are totally different languages and environments. You've linked to Java documents. I've fixed the tag for you. – T.J. Crowder Aug 24 '23 at 18:12
  • Would use Java's built-in `DateTimeFormatter` to convert both to `LocalDate` as answered in [java - String to LocalDate](https://stackoverflow.com/questions/8746084/string-to-localdate). Then `labOrderDate.compareTo(startDate)` (no times) will return -1 or 0 or +1 (see [`LocalDate.compareTo`](https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#compareTo(java.time.chrono.ChronoLocalDate))). It can be used to return your numbers. – hc_dev Aug 24 '23 at 18:59
  • 1
    Put those further details in your Question, not in Comments. – Basil Bourque Aug 24 '23 at 20:24
  • You're starting with strings, so compare as strings: `labOrderTime == start.substr(0,8)`. – RobG Aug 25 '23 at 04:14

1 Answers1

0

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");   
        }   
    }
}
aled
  • 21,330
  • 3
  • 27
  • 34