0

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

4
  • 2
    "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 input String otherwise it won't work. I'd also encourage you to stop using the java.util based date/time classes and make use of the newer java.time APIs instead Commented May 12, 2023 at 0:46
  • 3
    I'm assuming your trying to present nano seconds and not milliseconds, which SimpleDateFormat doesn't support, instead you should be trying to use something more like LocalDateTime.parse(dateService, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).format(DateTimeFormatter.ofPattern("dd/MM/yyyy, HH:mm:ss.nnnnnnnn")) Commented May 12, 2023 at 0:52
  • 2
    I strongly recommend you don’t use SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use LocalDateTime and DateTimeFormatter, both from java.time, the modern Java date and time API. Commented May 12, 2023 at 2:58
  • 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 yields 13-03-2023 12:00:02.000000000. Commented May 13, 2023 at 14:31

1 Answer 1

0
static String formatoFechaFinBd(String dateService) throws ParseException {
        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateService);
        String formattedDate = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSSSSSSSS").format(date);
        return formattedDate;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for wanting to contribute. Your way of doing it is strongly discouraged. We should all have stopped using SimpleDateFormat a long time ago since it is so troublesome to work with. Use java.time, the moddern java date and time API. And when you use its DateTimeFormatter, beware of the case of format pattern letters. You don’t want lower case hh here.
also using SimpleDateFormat, as posted here, will not work correctly if using hh (12-hour format) (e.g. formatoFechaFinBd("2022-10-20 15:16:17") will return "20-10-2022 03:16:17.000000000") || SSSSSSSSS would also be wrong if input format had milliseconds

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.