try { SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); Date date = dateFormat.parse("2022-01-01"); } catch (ParseException e) { System.out.println("Unable to parse date: " + e.getMessage()); e.printStackTrace(); }
try { DateFormatSymbols symbols = new DateFormatSymbols(new Locale("en", "US")); SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", symbols); Date date = dateFormat.parse("04 Feb 2022"); } catch (ParseException e) { e.printStackTrace(); }In this example, we are using a custom locale and date format to parse a date string. If the input string is not in the expected format, a ParseException will be thrown. We catch the exception and use the printStackTrace() method to output the full stack trace of the error.