Question

I'm developing an android application. This time format returns to me as a string from the database -->"PT20H" and I want to convert it to the following format --> "20:00". Any ideas?? Thanks in advance.

Was it helpful?

Solution

What you want to do is called parsing a time string. Take a look at SimpleDateFormat

Its seems like you've got some plain text that isn't informative, being the PT and the H. That is, if all your times are whole hours only (eg "PT20H23M" for 20:23 does not occur. I believe the pattern to use should be "'PT'H'H'" That is PT (between quotes to signify text to ignore, then the hour (symbol H) and then H between quotes again because it is to be ignored.

SimpleDateFormat sdf = new SimpleDateFormat("'PT'H'H'");
Date theDate = sdf.parse("PT20H");
SimpleDateFormat newFormat = new SimpleDateFormat("H:m");
String newFormattedString = newFormat.format(theDate);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top