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?
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?
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));
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.
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = format.parse(myString);