Fetch Date from String input . This method will extract date from string input and formatted into yyyy-MM-dd.
Input = "This is java nugget dated 03/24/2013"
output = 2013-03-24.
Input = "This is java nugget dated 03/24/2013"
output = 2013-03-24.
private static String formattedDate(String input) {
String formattedDate = "";
String DIGIT_REGX_4 = "([0-9]{4})";
String forwardSlash = "/";
String hyphen = "-";
int tr = 0;
if (input.contains(forwardSlash)) {
tr = input.indexOf(forwardSlash);
} else if (input.contains(hyphen)) {
tr = input.indexOf(hyphen);
}
if (tr != 0) {
String yString = input.substring(tr - 4, tr);
String day = "";
String month = "";
String year = "";
if ((yString.matches(DIGIT_REGX_4))) {
year = yString;
String xString = input.substring(tr + 1, tr + 3);
int int1 = Integer.parseInt(xString);
if (int1 > 12) {
day = xString;
month = input.substring(tr + 4, tr + 6);
} else {
month = xString;
day = input.substring(tr + 4, tr + 6);
}
formattedDate = year.concat(hyphen).concat(month).concat(hyphen).concat(day);
} else {
String xString = input.substring(tr - 2, tr);
int int1 = Integer.parseInt(xString);
if (int1 > 12) {
day = xString;
month = input.substring(tr + 1, tr + 3);
year = input.substring(tr + 4, tr + 8);
} else if (int1 <= 12) {
month = xString;
day = input.substring(tr + 1, tr + 3);
year = input.substring(tr + 4, tr + 8);
}
formattedDate = year.concat(hyphen).concat(month).concat(hyphen).concat(day);
}
}
return formattedDate;
}
No comments:
Post a Comment