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

Root93
  • 141
  • 8
  • 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 – MadProgrammer May 12 '23 at 00: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"))` – MadProgrammer May 12 '23 at 00: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](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 12 '23 at 02: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`. – Ole V.V. May 13 '23 at 14:31

1 Answers1

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;
    }
baoyinlei
  • 9
  • 1
  • 3
    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](https://docs.oracle.com/javase/tutorial/datetime/index.html). And when you use its `DateTimeFormatter`, beware of the case of format pattern letters. You don’t want lower case `hh` here. – Ole V.V. May 12 '23 at 03:04
  • 1
    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 – user16320675 May 12 '23 at 07:02