'issue program not working with day dates above 09

I have debugged and gotten my previous program partially working, but it refuses to give a correct output unless the day date is below 10. This program's purpose is to parse the date from a string. Excuse the bad naming and improper formatting, that is something I will work on after I get this working. I am using netbeans IDE 12.5.

package strings;

import javax.swing.JOptionPane;
import java.lang.*;
public class Strings {
    public static void main(String[] args) {
        String dateyear, datemonthnum, dateday, dateformat, slashcheck, datemonth;
        int slasharray[] = new int[2];
        int i, iplus;
        iplus = 0;
        dateformat = JOptionPane.showInputDialog("Please enter the date in mm/dd/yyyy format.");
        i = dateformat.indexOf('/');
        while (i >= 0) {
            System.out.println(i);
            System.out.println(iplus);
            System.out.println(slasharray[iplus]);
            slasharray[iplus] = i;
            iplus++;
            i = dateformat.indexOf('/', i + 1);
        }
        dateyear = dateformat.substring(slasharray[1]+1, 10);
        datemonthnum = dateformat.substring(slasharray[0]+1, slasharray[1]);
        dateday = dateformat.substring(0, slasharray[0]);
        System.out.println(dateyear);
        System.out.println(datemonthnum);
        System.out.println(dateday);
        if(datemonthnum.equals("01")){
            datemonth = "January";
        }
        else if(datemonthnum.equals("02")){
            datemonth = "February";
        }
        else if(datemonthnum.equals("03")){
            datemonth = "January";
        }
        else if(datemonthnum.equals("04")){
            datemonth = "April";
        }
        else if(datemonthnum.equals("05")){
            datemonth = "May";
        }
        else if(datemonthnum.equals("06")){
            datemonth = "June";
        }
        else if(datemonthnum.equals("07")){
            datemonth = "July";
        }
        else if(datemonthnum.equals("08")){
            datemonth = "August";
        }
        else if(datemonthnum.equals("09")){
            datemonth = "September";
        }
        else if(datemonthnum.equals("10")){
            datemonth = "October";
        }
        else if(datemonthnum.equals("11")){
            datemonth = "November";
        }
        else if(datemonthnum.equals("12")){
            datemonth = "December";
        }
        else {
            datemonth = "0";
        JOptionPane.showMessageDialog(null, "You have not entered a valid month. Please try again.");
        System.exit(0);
        }
        JOptionPane.showMessageDialog(null, "The Date is " + datemonth + " " + dateday + ", " + dateyear + ".");
    }

}


Solution 1:[1]

Can you use Java existing classes for the purpose?

File: Strings.java

package strings;

import javax.swing.JOptionPane;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Strings {

    public static String DATE_FORMAT = "MM/dd/yyyy";

    public static LocalDate parse(String dateStr) throws DateTimeParseException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
        return LocalDate.parse(dateStr, formatter);
    }

    public static void main(String[] args) {
        String dateInput;
        dateInput = JOptionPane.showInputDialog("Please enter the date in mm/dd/yyyy format.");

        try {
            LocalDate localDate = parse(dateInput);
            JOptionPane.showMessageDialog(null, "The Date is " + localDate.getMonth().name() + " " + localDate.getDayOfMonth() + " " + localDate.getYear());
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "You have not entered a valid month. Please try again.");
            System.exit(0);
        }

    }

}

input output

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1