An alternative (in addition to substring
already suggested in the other answer) would use split
:
String monthAndYear = ...
String[] partes = monthAndYear.split("/"); // separar pela barra
if (partes.length == 2) {
int month = Integer.parseInt(partes[0]);
int year = Integer.parseInt(partes[1]);
}
Remembering that the method parseInt
can launch a NumberFormatException
if one of the parties is not a number (and you can place a try
/catch
to check this if you want).
And you still need to validate the values (if the month is between 1 and 12, for example), which can be done before with regex (as suggested the other answer), or after the Parsing.
If you’re dealing with dates, use a date API
If his String
should have a date (though incomplete as it only has month and year), so why not use a date API?
If using Java >= 8, use the package classes java.time
:
String monthAndYear = ...
DateTimeFormatter parser = DateTimeFormatter.ofPattern("MM/uuuu");
YearMonth ym = YearMonth.parse(monthAndYear, parser);
int month = ym.getMonthValue();
int year = ym.getYear();
The class java.time.format.DateTimeFormatter
takes as a parameter the format in which the String
is (in this case, "month/year"). Next we use a java.time.YearMonth
, which is a class that represents a year and a month (just what we need).
The method parse
returns an instance containing the numerical values of the month and year that were in the String
. It is then possible to recover these numerical values using the respective getters. The Parsing also already validates if the values are valid (if the month is between 1 and 12), so you already guarantee that it is a valid date. Case to String
is not in the specified format, or some of its values are invalid, a DateTimeParseException
.
For Java <= 7, you can use the class java.text.SimpleDateFormat
, which works in a similar way to java.time
:
String monthAndYear = ...
SimpleDateFormat sdf = new SimpleDateFormat("MM/yyyy");
sdf.setLenient(false);
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(monthAndYear));
int month = c.get(Calendar.MONTH) + 1;
int year = c.get(Calendar.YEAR);
An important difference is the use of setLenient(false)
, otherwise months with values such as "00" and "13".
Case to String
is invalid, a ParseException
.
Another detail is that to get the numeric value of the month you must add up to 1, because in this API the months are indexed to zero (January is zero, February is 1, etc).
If you want, for Java 6 and 7 you can also use Threeten Backport, one backport of java.time
. Basically, it has classes and methods with the same names and functionalities, the difference is that instead of using the package java.time
, you use the org.threeten.bp
. Apart from this detail, the code would look just like the Java 8 example above.
what error? show the value of the monthAndYear variable
– Ricardo Pontual
To get the month you have to use monthAndYear.substring(0, 2), are two digits required by the mask that presents.
– MauroAlmeida
Before performing parseint you should also check whether the monthAndYear variable has the format you wanted it to be inserted and otherwise indicate to the user that you have made a mistake and ask again.
– MauroAlmeida