0

So far I have the first month figured out but I still need help with the day and year. I am having trouble parsing the individual pieces and converting them to integers.

int firstSlash = date.indexOf ("/"); month = Integer.parseInt (date.substring (0, firstSlash));

That is what I have so far.

  • You can check this article https://stackoverflow.com/questions/18099285/split-date-time-strings – GMD Oct 08 '20 at 23:56
  • Use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). After `LocalDate ld = LocalDate.parse("9/27/2020", DateTimeFormatter.ofPattern("M/d/u"));` you may use the `getYear`,`getMonthValue` and `getDayOfMonth` methods of `ld` to get the numbers for year, month and date. – Ole V.V. Oct 09 '20 at 05:10
  • On the occasion of your question I wrote [a new and modern answer to one of the linked original quesitons here](https://stackoverflow.com/a/64281773/5772882). – Ole V.V. Oct 09 '20 at 14:16

2 Answers2

2

Easy way

There is a function called split() that takes the delimiter and returns an array of strings:

String[] words = date.split("/");
int month = Integer.parseInt(words[0]);
int day = Integer.parseInt(words[1]);
int year = Integer.parseInt(words[2]);

Correct way

When it comes to parsing date from string, the preferred way is using Java DateFormat API:

DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date theDate = format.parse(date);

Date object is much more powerful and allows you to interact with date and time much more fluently than bare ints

More info here!

DelfikPro
  • 659
  • 3
  • 8
  • I beg to differ. Leaving to a good library is easier than hand parsing (splitting and parsing individual ints). And please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Oct 09 '20 at 05:01
0

If you know where are your month, year and day like : 25/10/2020 you can just use the split function

If you are SURE that the date will be in the right format :

String[] dateSplit = date.split("/");
int day = Integer.valueOf(dateSplit[0]);
int month = Integer.valueOf(dateSplit[1]);
int year = Integer.valueOf(dateSplit[2]);
System.out.println("year" + year);
System.out.println("month" + month);
System.out.println("day" + day);

If you are NOT SURE that the date will be in the right format :

String[] dateSplit = date.split("/");
if (dateSplit.length != 3) {
    throw new Exception("Date not in valid format");
    //or do something else like printing or whatever...
}

try {
    int day = Integer.valueOf(dateSplit[0]);
    int month = Integer.valueOf(dateSplit[1]);
    int year = Integer.valueOf(dateSplit[2]);
    System.out.println("year" + year);
    System.out.println("month" + month);
    System.out.println("day" + day);
} catch (NumberFormatException e) {
    throw new Exception("Date not in valid format");
    //or do something else like printing or whatever...
}