0

Is there any way that I could convert time in this format 2640 Hrs : 00 Mts to 14:22:57 hrs.

I tried a lot of ways and nothing seems to be working. I really need help.

Json Array

{"ElapsedTime":"2642 Hrs : 59 Mts"}

Code I tried

DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
Date date = dateFormat.parse(jsonDate);
return dateFormat.format(date);

The above code throws this exception

java.text.ParseException: Unparseable date: "2640 Hrs : 00 Mts" (at offset 4)

I'm using Volley GsonRequest and I'm able to parse it but unable to format time.

Gurupriyan
  • 51
  • 9
  • This is not *time*, but rather *duration*. In java8 you can use class java.time.Duration. But you still need to parse it manually. – agad Sep 04 '14 at 12:40
  • possible duplicate of [How to format a duration in java? (e.g format H:MM:SS)](http://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss) – agad Sep 04 '14 at 12:41
  • Seems you're missing the moment from when it is elapsed (elapsed since when?)... what are you going to do with that time? – Jonathan Drapeau Sep 04 '14 at 12:50

1 Answers1

1

To me that is not a known time format, and is not "normalized", meaning it gives you 2642 hours, that doesn't fit into a 24 hours clock.

So I would go with manual parsing, something like :

Pattern pattern = Pattern.compile("([0-9]*) Hrs : ([0-9]*) Mts");
Matcher matcher = pattern.matcher(jsonString);
int hours = Integer.parseInt(matcher.group(1));
int minutes = Integer.parseInt(matcher.group(2));
long time = (hours * 60) + minutes; // In minutes
time *= 60000; // In milliseconds
return dateFormat.format(new Date(time));

I have not tested this code, so YMMV, but you should get the idea.

Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
Simone Gianni
  • 11,426
  • 40
  • 49
  • try it: *SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss"); System.out.println(format.format(new Date(26 * 60 * 60 * 1000)));* Result is: 03:00:00 – agad Sep 04 '14 at 12:57
  • I tried it, but it throws an exception java.lang.IllegalStateException: No successful match so far java.util.regex.Matcher.ensureMatch(Matcher.java:596) java.util.regex.Matcher.group(Matcher.java:357) – Gurupriyan Sep 10 '14 at 06:20