0

Hi I have time string format : 2017-02-20 09:57:08.512534+00

How to change above format to "dd//MM/yyyy" format in java?

VijayD
  • 826
  • 1
  • 11
  • 33
Sai's Stack
  • 1,345
  • 2
  • 16
  • 29

3 Answers3

2

Convert String to date object using parse method then format date object using format method as per your requirement

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
Date date = format1.parse("2017-02-20 09:57:08.512534+00");
System.out.println(format2.format(date));
Snehal Patel
  • 1,282
  • 2
  • 11
  • 25
1

Do yourself the favour of using the Java 8 java.time classes if you can use Java 8:

    String dateTime = "2017-02-20 09:57:08.512534+00";
    String date = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSx"))
            .format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
    System.out.println(date);

This prints

20/02/2017

At face value it looks pretty much the same as the version using the now obsolete classes (Date and SimpleDateFormat). Still. For one thing, for your own good you will want to learn to use the new classes, not the legacy ones. Also, if some day you want to something else with the date than just convert from one string representation to another, the versatility and wealth of options of LocalDateTime and friends is likely to be useful.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = format.parse(myString);
Saqib Javed
  • 120
  • 9
  • Thanks for quick reply. But I am getting "java.text.ParseException". I am doing below way to do it: SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Date date = formatter.parse(updatedTime); return simpleDateFormat.format(date); – Sai's Stack Feb 20 '17 at 11:50