Converting time in java with string manipulation -



Converting time in java with string manipulation -

how can convert 2m0s 00:02:00 in java? both string.

i tried string , char manipulation unsuccessful.

i did in php function

but need convert java.

preg_match_all('/(\d+)([hms])/', $sections[2], $parts, preg_set_order); $units = array('h' => 'hours', 'm' => 'minutes', 's' => 'seconds'); foreach($parts $part) { $result[$units[$part[2]]] = $part[1]; }

sure, can regex, more fool proof solution utilise language's date api.

for java mean using simpledateformat:

string input = "2m0s"; simpledateformat parseformat = new simpledateformat("mm'm'ss's'"); date date = parseformat.parse(input); simpledateformat displayformat = new simpledateformat("mm:ss"); string output = displayformat.format(date); system.out.println(input + " -> " + output);

which print: 2m0s -> 02:00 console. nice thing input "2m75s" converted "03:15".

more info on how build pattern parse strings, or format dates, can found here: http://docs.oracle.com/javase/7/docs/api/java/text/simpledateformat.html

java

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -