I have a String that receives the following date format (yyyy-MM-dd ...)
"2023-03-13 12:00:02"
and i need to change the format to the following (dd-MM-yyyy ...)
"13-03-2023 12:00:02.000000000"
I tried to build the following method but it doesn't work and I don't have much idea how it should be
String formatoFechaFinBd(String dateService) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(dateService);
String formattedDate = new SimpleDateFormat("dd/MM/yyyy, Ka").format(date);
return formattedDate;
}
How can I do the conversion I need to get that date format in the String
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"is not the same as"2023-03-13 12:00:02"- you parsing format needs to match the expected inputStringotherwise it won't work. I'd also encourage you to stop using thejava.utilbased date/time classes and make use of the newerjava.timeAPIs insteadSimpleDateFormatdoesn't support, instead you should be trying to use something more likeLocalDateTime.parse(dateService, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).format(DateTimeFormatter.ofPattern("dd/MM/yyyy, HH:mm:ss.nnnnnnnn"))SimpleDateFormatandDate. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead useLocalDateTimeandDateTimeFormatter, both from java.time, the modern Java date and time API.LocalDateTime .parse("2023-03-13 12:00:02", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")) .format(DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss.SSSSSSSSS")). It yields13-03-2023 12:00:02.000000000.