I have the code:
String str="Date-01-2017"
How can I print 2017 only from this string?
I don't know how to split and get the return as 2017.
I have the code:
String str="Date-01-2017"
How can I print 2017 only from this string?
I don't know how to split and get the return as 2017.
You can try:
String[] splitString = dateString.split("-");
String year = splitString[2];
You can use substring
method to take the part of the string you need:
String data = "Date-01-2017";
String year = data.substring(data.length() - 4, data.length());
You could use regular expression:
Pattern pattern = Pattern.compile("\\w+-(?<month>\\d{2})-(?<year>\\d{4})");
Matcher matcher = pattern.matcher("Date-01-2017");
if(matcher.matches())
System.out.println(matcher.group("year"));
else
System.out.println("NOT MATCH!!!");
Another way to print if the string "2017" is using a function, such as this.
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
You can make a call to this function, with the string as the parameter, and if the string contains 2017, then u can print out that string.