-2

I'm trying to convert a String month to a int month in Java. I do not want my program to have a lot of logic on it, so I'm trying to do it without having to create a switch case or a Enum. If not possible, i'll have to do deal with it and create that logic...

My String Sample is this:

String date = "2017-Oct-27";

And I want it to be like this:

String date = "2017-10-27";

Can anyone help me please? Thanks

1 Answers1

1

Please try below code

String input = "2017-Oct-27";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MMM-dd");
String formattedDate= "";
Date date;
try {
    date = parser.parse(input);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    formattedDate = formatter.format(date);
} catch (ParseException e1) {
    e1.printStackTrace();
}