I get datetime data from soap web service to get string:2020-05-03T00:00:00. Is there a way to split the string into dd / mm / yyyy, and if it is null then omitted? I am using a substring(0,10) , but it doesn't work very well.
Asked
Active
Viewed 2,592 times
0
-
1Does this answer your question? [How to format date and time in Android?](https://stackoverflow.com/questions/454315/how-to-format-date-and-time-in-android) – Jaymin May 12 '20 at 08:44
-
And what language are you programming this in any way? What you got is an ISOformatted Date, and every language has date converters and formatters to deal with that. But if you don't tell us... Then you have to do your own research. – Kasey Chang May 12 '20 at 08:47
-
My questions are quite similar, but the answers do not solve my problem. – Thien Nguyen May 12 '20 at 08:49
-
Welcome to Stack Overflow. Please check out [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) once more. In your question please report what your search has brought up and how it was unhelpful or insufficient. Then we’ll understand your situation much better and can help you much better. To quote the link: *including links to related questions that* haven't *helped can help others in understanding how your question is different from the rest.* – Ole V.V. May 12 '20 at 15:58
3 Answers
3
You can do it this way.
Parse the input string.
LocalDateTime dateTime = LocalDateTime.parse("2020-05-03T00:00:00");
Generate text representing the value of that LocalDateTime
object.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String dateTimeString = dateTime.format(formatter);
System.out.println(dateTimeString);
03/05/2020
For early Android before 26, see the ThreeTenABP & ThreeTen-Backport projects, a back-port of most of the java.time functionality.

Basil Bourque
- 303,325
- 100
- 852
- 1,154

doublezofficial
- 111
- 8
0
try with this
try {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
date = inputFormat.parse("2020-05-03T00:00:00");
String formattedDate = outputFormat.format(date);
System.out.println("coverted: "+formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: 03-05-2020

Flying Dutchman
- 365
- 6
- 13
-
These terrible date-time classes were years ago supplanted by the *java.time* classes defined in JSR 310. See the modern solution in the [Answer by Zameer](https://stackoverflow.com/a/61747768/642706). – Basil Bourque May 12 '20 at 14:34
0
You can use java Calendar class to get Date, Month and Year as shown in below
String s = "2020-05-03T00:00:00";
Calendar calendar = new GregorianCalendar();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateObj = null;
try {
dateObj = format.parse(s);
calendar.setTime(dateObj);
int date = calendar.get(Calendar.DATE);
int month = calendar.get(Calendar.MONTH) + 1;// As start from 0
int year = calendar.get(Calendar.YEAR);
System.out.println("Formatted Date -> "+ date + "/" + month + "/" +year);
} catch (ParseException e) {
e.printStackTrace();
}
You can do many more thing after converting it to Calendar class object, like hour, minutes, day of week etc

Swetank
- 1,577
- 11
- 14