Get the Month from a date string

1.6k views Asked by At

I have a String input of date "10/10/2020" (assume they are always going to be separated with /) and I am trying to store each category in a private int variable already defined as Month, Day, and year.

I used the parseInt, indexOf, and substring methods to find the month and store it in the month variable but I am having a hard time figuring out how I can have the program read the day. We are to assume that the month and day can be in "00" or "0" formats.

This is how I am reading the month from the string and this is what I got so far for reading the day but I am getting an error.

java.lang.StringIndexOutOfBoundsException: begin 2, end 0, length 8

This is my code so far please let me know what mistake I am doing.

    int firstSlash = date.indexOf("/");
    int secondSlash = date.indexOf("/", firstSlash);
    
    month = Integer.parseInt (date.substring (0, firstSlash));
    day = Integer.parseInt (date.substring(firstSlash+1, secondSlash-1));
    

I don't want the answer but please help me understand the logic of where I am going wrong because based on my understanding I seem to be getting the Index values between the first and second slash and converting the String value into an int.

3

There are 3 answers

4
Eklavya On BEST ANSWER
  • First you need to search from next index of first / found means you should use firstSlash + 1 instead firstSlash in date.indexOf("/", firstSlash).
  • .substring()'s 2nd paramerter is exclusive, so you need to use secondSlash instead of secondSlash-1 in date.substring(firstSlash+1, secondSlash-1).

Your code should be like

String date = "10/10/2020";
int firstSlash = date.indexOf("/");
int secondSlash = date.indexOf("/", firstSlash + 1);

int month = Integer.parseInt (date.substring (0, firstSlash));
int day = Integer.parseInt (date.substring(firstSlash+1, secondSlash));

It's better to use LocalDate to store date and use DateTimeFormatter to parse the date.

0
Arvind Kumar Avinash On

The idiomatic way of doing it is by using the date-time API e.g. LocalDate and DateTimeFormatter as shown below:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String strDate = "10/10/2020";
        LocalDate date = LocalDate.parse(strDate, DateTimeFormatter.ofPattern("M/d/u"));
        int year = date.getYear();
        int month = date.getMonthValue();
        int dayOfMonth = date.getDayOfMonth();

        System.out.println("Year: " + year + ", Month: " + month + ", Day of month: " + dayOfMonth);
    }
}

Output:

Year: 2020, Month: 10, Day of month: 10

Learn more about the modern date-time API at Trail: Date Time.

0
Ole V.V. On

I don't want the answer but please help me understand the logic of where I am going wrong because …

That's the attitude I and we like, the attitude of a true learner. I will provide useful information and links, not solve your error for you.

  1. Like others I recommend that you use java.time, the modern java date and time API, for your date work.
  2. You sem to have some indexing inaccuracies in your code.

java.time

Use the LocalDate class of java.time for a date and its getMonth or getMonthValue method for getting the month. java.time can parse a date string where month and day can be in "00" or "0" formats. If you try it and run into doubts, a new question from you is always welcome.

What went wrong in your code

This should suffice for identifying the flaws in your code:

  1. To start with what I think you already know well: indices in Java are 0-based.
  2. The two-argument String.indexOf(String, int) searches the string from the given index inclusive. If the substring searched for is at the index you specify, the same index is returned.
  3. The two-argument substring(int, int) gives you the substring from the from-index inclusive to the to-index exclusive. The to-index needs to be at least as great as the from-index, or a StringIndexOutOfBoundsException will be thrown. The exception message mentions the two indices that you passed to substring(), which may give you a chance to think backward to where they came from.

Reservation

I know I'm late at the party. I still couldn't resist posting what I think is a good answer.

Links