Skip to content

Instantly share code, notes, and snippets.

@Edclydson
Created December 30, 2022 14:29
Show Gist options
  • Save Edclydson/e040085b755e6fad7ff352fd1adad74e to your computer and use it in GitHub Desktop.
Save Edclydson/e040085b755e6fad7ff352fd1adad74e to your computer and use it in GitHub Desktop.
Método para converter uma data inserida pelo User (String) no formato (dd-MM-yyyy) e devolve-la com o tipo Date (mesmo tipo que está no DB) e no formato (yyyy-MM-dd)
public Date converteStringtoData(String data) {
try{
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
DateTimeFormatter dataFormatoInserido = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter dataFormatoNecessario = DateTimeFormatter.ofPattern("yyyy-MM-dd");
date.setLenient(false);
return date.parse(LocalDate.parse(data, dataFormatoInserido).format(dataFormatoNecessario));
} catch (DateTimeParseException | NullPointerException | ParseException e){
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment