1

essentially, what I am doing is getting a date such as "4/18/1972" and regurgitating it properly formatted "April 18, 1972". I was able to get past the first hurdle,

Here's the code I have initially to build it:

    System.out.print("Date: ");
    receivedDate = user.next();

    String receivedDateMonth = receivedDate.substring(0, receivedDate.indexOf("/"));
    int receivedMonth = Integer.parseInt(receivedDateMonth);    

    if(month >= 1 && month <= 9)
    {
        String receivedDateDay = receivedDate.substring(2, receivedDate.indexOf("/"));
        int day = Integer.parseInt(receivedDateDay);
        System.out.println(day);
    }

As I wrote it, I was testing to make sure receivedDateMonth appears properly and it did, but as I move onto the day, it doesn't work. I keep getting an error, "String index out of range: -1". I figure it's centered on the "2" there, so I changed its value and the problem persists. I put the 2 in because I wanted it to count from the first digit, 0, then the "/", with the intention of starting the next parse after that(I would change the 10-12 to do similar, go to 3), however, for the life of me, I cannot figure this out. Could someone please point me in the right direction? Thanks a lot.

Vaak
  • 39
  • 6
  • The right way would be to use an inbuilt api like [SimpleDateFormat](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) – Codebender Oct 12 '15 at 03:13

1 Answers1

1

I found the answer here Parse String to Date with Different Format in Java , modified the date formats as per your need

MMMM for complete month in String

SimpleDateFormat fromUser = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat myFormat = new SimpleDateFormat("MMMM dd, yyyy");

try {    
    String reformattedStr = myFormat.format(fromUser.parse(inputString));
} catch (ParseException e) {
    e.printStackTrace();
}
Community
  • 1
  • 1
Saravana
  • 12,647
  • 2
  • 39
  • 57