-1

I use the code below to see if my input date (format mm/dd/yyyy HH:mm:ss) falls within the range of startDate and endDate.

I use compareTo() in this logic. But with the format mm/dd/yyyy, it compares the month alone and prints output in "MYLOGIC methood". But I need the year to be compared to see if the input date is within startDate and endDate range.

public class DateLogicNew {

    public static void main(String[] args) {
        String startDate[] = new String[1];
        String endDate[] = new String[1];
        startDate[0] = "01/01/0600 00:00:00";
        endDate[0] = "11/27/3337 00:00:00";

        String inputArr[] = { "05/01/0500 01:00:00", "11/27/3337 00:00:00",
                "05/05/0700 00:00:00", "11/27/2337 00:00:00",
                "06/05/4000 00:00:00" };

        String protectedArr[] = new String[inputArr.length];
        int temp[] = new int[inputArr.length];

        System.out.println(inputArr.length);

        System.out.println("Length of the inputArr: " + inputArr.length);
        // System.out.println("");

        for (int i = 0; i < inputArr.length; i++) {

            if (inputArr[i]
                    .matches("^([0-1][0-9])/([0-3][0-9])/([0-9]{4})(?:( [0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$")) {
                System.out.println("Inside if loop");

                if (inputArr[i].compareTo(startDate[0]) > 0
                        && inputArr[i].compareTo(endDate[0]) < 0) {
                    System.out.println("inside the compare condition");
                    temp[i] = 1;
                    protectedArr[i] = inputArr[i];
                    System.out
                            .println("Values of the inputArr in MYLOGIC method : "
                                    + protectedArr[i]);
                }
            } else {
                temp[i] = 0;
            }
        }
        System.out.println("");

        for (int i = 0; i < inputArr.length; i++) {
            if (temp[i] == 1) {
                inputArr[i] = protectedArr[i];
            }
            System.out
                    .println("Final Value to the output port: " + inputArr[i]);
        }
    }
}
Megu
  • 5
  • 4
  • 1
    You need to be clearer in describing your problem - what happens, what you expect to happen, how they differ, some sample input and output, etc. You also might find it easier to use the built-in date parsing and handling libraries. – pvg Oct 04 '17 at 15:06
  • 1
    Why don't you use actual `Date` instances to do the comparison? Comparing string based dates is bound to be tricky. String comparisons are done on a character-by-character basis and thus `10/31/9999` would still be "smaller" than `11/27/3337`, because the chars at index 1 ( 0 and 1) are compared independent from the rest. – Thomas Oct 04 '17 at 15:08
  • Do you really care about 1 AM in the year 500? Or is something else going on here? This example data is strange. – Basil Bourque Oct 04 '17 at 21:43

2 Answers2

0

java.time

Parse as date-time objects. Regex is overkill.

The modern approach uses the java.time classes rather than the old legacy date-time classes.

String input = "05/01/0500 01:00:00" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

Compare using methods isBefore, isAfter, isEqual.

if( ( ! x.isBefore( start ) ) && x.isBefore( stop ) ) { … }
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I myself figured out the solution for my question. Please check the code below to see the logic before comparing the dates.

String startDate = "0600/01/01 00:00:00"; String endDate = "3337/11/27 00:00:00";

    try {

        for (int i = 0; i < inputArr.length; i++) {
            String newDateInput = inputArr[i];
            // System.out.println("NewInput:" + newInput);

            DateFormat parser = new SimpleDateFormat("MM/dd/yyyy");
            DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");

            Date convertedDate = parser.parse(newDateInput);
            String newFormattedInput = formatter.format(convertedDate);
            // System.out.println("newFormattedInput: " +
            // newFormattedInput);
            if (newFormattedInput
                    .matches("^([0-9]{4})/([0-1][0-9])/([0-3][0-9])(?:( [0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$")) {
                if (newFormattedInput.compareTo(startDate) > 0
                        && newFormattedInput.compareTo(endDate) < 0) {
                    temp[i] = 1;

                    protectedArr[i] = inputArr[i];
                    System.out
                            .println("Values of the inputArr in PROTECT method : "
                                    + protectedArr[i]);

                } else {
                    temp[i] = 0;
                }
            }
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
Megu
  • 5
  • 4