-5

Well I tried to look for many questions but couldn't find something relevant. I have a string that has the following data:

String sDate = "2018-01-17 00:00:00";

This comes from an application, and I need to convert it into the following Date format

17-01-2018

I went through this link but could not relate.

Can someone help..?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user9228823
  • 9
  • 1
  • 2
  • 2
    do you need a date object or just change the presentation of the string? – XtremeBaumer Jan 17 '18 at 10:16
  • If you want a date Object you could use https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Ben Sch Jan 17 '18 at 10:17
  • Welcome to Stack Overflow! Please [edit] your question to show [the code you have tried](http://whathaveyoutried.com), and how its output differs from what you want. You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Jan 17 '18 at 10:18
  • Possible duplicate of [Changing String date format](https://stackoverflow.com/questions/34846358/changing-string-date-format) – Ole V.V. Jan 17 '18 at 10:54
  • 1
    Good that you found one apparently related question already. There are many, many more, please continue searching. I am sure your answer is out there already. See for example [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion). – Ole V.V. Jan 17 '18 at 10:54
  • 1
    No, @BenSch, please don’t use that class, `SimpleDateFormat`. It is not only long outdated, it is also notoriously troublesome. [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) is so much nicer to work with. – Ole V.V. Jan 17 '18 at 11:51

3 Answers3

5

If you are using Java 8, you can use java.time library and :

String sDate = "2018-01-17 00:00:00";

//Step one : convert the String to LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(sDate, formatter);

//Step two : format the result date to dd-MM-yyyy
String result = date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));

Output

17-01-2018

Another Overengineering solution (It works just in your case) you can benefits from the default format of LocalDateTime :

String result = LocalDateTime.parse(sDate.replace(" ", "T"))
        .format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 3
    This is the good and modern solution. If you are not yet using Java 8 or 9, you may still do it the same way when you add [ThreeTen Backport](http://www.threeten.org/threetenbp/) to your project. – Ole V.V. Jan 17 '18 at 10:58
2
public static void main(String args[]) {  
String sDate = "2018-01-17 00:00:00";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = df.parse(sDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
        System.out.println(df1.format(date));

}


it should solve your problem.
Jabongg
  • 2,099
  • 2
  • 15
  • 32
  • 4
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 17 '18 at 10:50
0

You need to use the SimpleDateFormat:

    String sDate = "2018-01-17 00:00:00";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = df.parse(sDate);

    SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
    System.out.println(df1.format(date));

Go through the SimpleDateFormat class to get into details for this