title: Dealing with StringIndexOutOfBoundsException even if the specified field is empty
body: <p><a href="https://i.stack.imgur.com/SYHgA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SYHgA.jpg" alt="enter image description here"></a>I'm getting an app crash that happens when I press the Submit button of my android application.
  The application deals with sending the entered data to be added as an event in the user's calendar.
  The app crashes only when the date field is left empty.
  I've noticed that there is a java.lang.StringIndexOutOfBoundsException for the variables which store the split parts of the date.I tried commenting the date field data input and then the app works.</p>
  
  <p>Why does this exception occur if the said field is left empty?</p>
  
  <p>Any fixes will be appreciated.</p>
  
  <p>Code:</p>
  
  <pre><code>public void onClick(View view) {
  
          TextInputEditText textInputEditTextDate = (TextInputEditText) findViewById(R.id.date);
          EditText editTextTitle = (EditText) findViewById(R.id.title);
          EditText editTextDesc = (EditText) findViewById(R.id.desc);
          EditText editTextLocation = (EditText) findViewById(R.id.location);
          titl = editTextTitle.getText().toString();
          desc = editTextDesc.getText().toString();
          location = editTextLocation.getText().toString();
          date = textInputEditTextDate.getText().toString();
          day = Integer.parseInt(date.substring(0, 2));
          month = Integer.parseInt(date.substring(3, 5)) - 1;
          year = Integer.parseInt(date.substring(6, 10));
  
          if (titl.isEmpty() || desc.isEmpty() || location.isEmpty() || date.isEmpty()) {
              Toast.makeText(this, "No information", Toast.LENGTH_SHORT).show();
          } else {
              Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
              calendarIntent.putExtra(CalendarContract.Events.TITLE, titl);
              calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, location);
              calendarIntent.putExtra(CalendarContract.Events.DESCRIPTION, desc);
              calendarIntent.putExtra(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_DEFAULT);
              calendarIntent.putExtra(CalendarContract.Reminders.MINUTES, 1);
              calendarIntent.putExtra(String.valueOf(CalendarContract.Reminders.METHOD_ALERT), "1");
              GregorianCalendar date = new GregorianCalendar(year, month, day);
              calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, date.getTimeInMillis());
              calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, date.getTimeInMillis());
              startActivity(calendarIntent);
          }
      }
  </code></pre>
  
  <p>Logcat:</p>
  
  <pre><code>Process: com.cypherlabs.calendarapp, PID: 32057
      java.lang.IllegalStateException: Could not execute method for android:onClick
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
          at android.view.View.performClick(View.java:7333)
          at android.widget.TextView.performClick(TextView.java:14160)
          at android.view.View.performClickInternal(View.java:7299)
          at android.view.View.access$3200(View.java:846)
          at android.view.View$PerformClick.run(View.java:27773)
          at android.os.Handler.handleCallback(Handler.java:873)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:214)
          at android.app.ActivityThread.main(ActivityThread.java:6981)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
       Caused by: java.lang.reflect.InvocationTargetException
          at java.lang.reflect.Method.invoke(Native Method)
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
          at android.view.View.performClick(View.java:7333) 
          at android.widget.TextView.performClick(TextView.java:14160) 
          at android.view.View.performClickInternal(View.java:7299) 
          at android.view.View.access$3200(View.java:846) 
          at android.view.View$PerformClick.run(View.java:27773) 
          at android.os.Handler.handleCallback(Handler.java:873) 
          at android.os.Handler.dispatchMessage(Handler.java:99) 
          at android.os.Looper.loop(Looper.java:214) 
          at android.app.ActivityThread.main(ActivityThread.java:6981) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445) 
       Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=2
          at java.lang.String.substring(String.java:2032)
          at com.cypherlabs.calendarapp.MainActivity.onClick(MainActivity.java:34)
          at java.lang.reflect.Method.invoke(Native Method) 
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
          at android.view.View.performClick(View.java:7333) 
          at android.widget.TextView.performClick(TextView.java:14160) 
          at android.view.View.performClickInternal(View.java:7299) 
          at android.view.View.access$3200(View.java:846) 
          at android.view.View$PerformClick.run(View.java:27773) 
          at android.os.Handler.handleCallback(Handler.java:873) 
          at android.os.Handler.dispatchMessage(Handler.java:99) 
          at android.os.Looper.loop(Looper.java:214) 
          at android.app.ActivityThread.main(ActivityThread.java:6981) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
  </code></pre>