Created
December 30, 2022 14:29
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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