-1

I want to convert String to a date field in Java. Currently, I have this string with me and I want to convert into date(MM/DD/YYYY) field. Any suggestion?

String  str = "2019-12-11 00:00:00"

The output I want is 11/12/2019.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Kap
  • 1
  • 1
    Hi and welcome to Stack Overflow! Please remember that this is not a free code writing service and you are required to show us what you've tried so far. Please [edit](https://stackoverflow.com/posts/58724685/edit) your post to add a [mre], a [Short, Self Contained, Correct Example](http://www.sscce.org) or at least show us what you've tried so far. Additionally, read [How to ask](https://stackoverflow.com/help/how-to-ask), a guide on asking questions on Stack Overflow efficiently so that other users may help you faster and better. – yur Nov 06 '19 at 07:03
  • 1
    As a side note, think about using `LocalDate` instead. The `Date` class is flawed. – Axel Nov 06 '19 at 07:21
  • I’m immodest enough to recommend [my own answer here](https://stackoverflow.com/a/48299652/5772882). See if you can’t adapt it to your situation (and if not, ask a new question specifying what you tried). – Ole V.V. Nov 06 '19 at 16:43

1 Answers1

-1

There are probably more elegant solutions for this problem, but this little method will suffice for your specific case:

private static String formatDate(String str) 
    {
        String[] split = str.split(" ");

        String[] split2 = split[0].split("-");

        String result = split2[2].concat("/").concat(split2[1]).concat("/").concat(split2[0]);

        return result;
    }
yur
  • 380
  • 2
  • 15