Solution using java.time
API:
I recommend you use java.time.Period
which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation.
Demo:
import java.time.Period;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String[] arr = { "1 years, 2 months, 22 days", "1 years, 1 months, 14 days", "4 years, 24 days",
"13 years, 21 days", "9 months, 1 day" };
List<Period> periodList =
Arrays.stream(arr)
.map(s -> Period.parse(
"P" + s.replaceAll("[\\s+,]", "")
.replaceAll("years?","Y")
.replaceAll("months?", "M")
.replaceAll("days?", "D")
)
)
.collect(Collectors.toList());
System.out.println(periodList);
// Now you can retrieve year, month and day from the Period e.g.
periodList.forEach(p ->
System.out.println(
p + " => " +
p.getYears() + " years " +
p.getMonths() + " months "+
p.getDays() +" days"
)
);
}
}
Output:
[P1Y2M22D, P1Y1M14D, P4Y24D, P13Y21D, P9M1D]
P1Y2M22D => 1 years 2 months 22 days
P1Y1M14D => 1 years 1 months 14 days
P4Y24D => 4 years 0 months 24 days
P13Y21D => 13 years 0 months 21 days
P9M1D => 0 years 9 months 1 days
ONLINE DEMO
Learn more about the modern Date-Time API* from Trail: Date Time.
Solution using Java RegEx API:
Another way of doing it can be by using Matcher#find
.
Demo:
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String[] arr = { "1 years, 2 months, 22 days", "1 years, 1 months, 14 days", "4 years, 24 days",
"13 years, 21 days", "9 months, 1 day" };
int[] years = new int[arr.length];
int[] months = new int[arr.length];
int[] days = new int[arr.length];
Pattern yearPattern = Pattern.compile("\\d+(?= year(?:s)?)");
Pattern monthPattern = Pattern.compile("\\d+(?= month(?:s)?)");
Pattern dayPattern = Pattern.compile("\\d+(?= day(?:s)?)");
for (int i = 0; i < arr.length; i++) {
Matcher yearMatcher = yearPattern.matcher(arr[i]);
Matcher monthMatcher = monthPattern.matcher(arr[i]);
Matcher dayMatcher = dayPattern.matcher(arr[i]);
years[i] = yearMatcher.find() ? Integer.parseInt(yearMatcher.group()) : 0;
months[i] = monthMatcher.find() ? Integer.parseInt(monthMatcher.group()) : 0;
days[i] = dayMatcher.find() ? Integer.parseInt(dayMatcher.group()) : 0;
}
// Display
System.out.println(Arrays.toString(years));
System.out.println(Arrays.toString(months));
System.out.println(Arrays.toString(days));
}
}
Output:
[1, 1, 4, 13, 0]
[2, 1, 0, 0, 9]
[22, 14, 24, 21, 1]
ONLINE DEMO
Explanation of the regex:
\d+
: One or more digits
(?=
: Start of lookahead assertion pattern
year
: A whitespace character followed by year
(?:s)?
: Optional character, s
)
: End of lookahead assertion pattern
Check this regex demo to understand the regex more closely.
* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time
.