Converting String to Date

Can't convert String to Date

Guided by this article, I managed to convert a date where days/months/years are passed in numbers, but when, for example, I enter the name of the month in letters, the error

Works:

SimpleDateFormat formatter = new SimpleDateFormat("dd MM yyyy");//задаю формат даты
String dateInString = "29 11 2015";//создаю строку по заданному формату
Date date = formatter.parse(dateInString);//создаю дату через 
System.out.println(formatter.format(date));

Does not work:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "28-Nov-2015";
Date date = formatter.parse(dateInString);
System.out.println(formatter.format(date));

Issues:

Exception in thread "main" java.text.ParseException: Unparseable date: "28-Nov-2015"
Author: Qwertiy, 2015-11-29

2 answers

Java tries to use the regional settings taken from the system. And there, most likely, the Russian language is used. In order for the parser to understand the English names of the months, it needs to be created a little differently:

new SimpleDateFormat("dd-MMM-yyyy", Locale.US);

Or write the names of the months in Russian:

String dateInString = "28-Ноя-2015";
 8
Author: , 2015-11-29 07:06:39
 public static String dateToString(Date date, String f) {                        
        if (date != null) {
            return new SimpleDateFormat(f, Locale.ENGLISH).format(date);
        }
        return null;
    }
 1
Author: Aslan Kussein, 2017-01-09 06:08:15