0

I have the following code and in need to replace the value on time from PT14H01M00S to 14:01:00 the following code is omit the first two char but I get 14H01M00S ,there is no way to do that without using string builder?

String time = "PT14H01M00S";
String substring = time .substring(2);
substring.replace("H", ":");
substring.replace("M", ":");
substring.replace("S", "");

System.out.println(substring);
1
  • It seems like you are trying to format a JodaTime Period into a different format. You should really be using the formatter given by JodaTime, rather than using String class API. See my answer for the correct way of doing this task. Commented Aug 11, 2013 at 11:37

8 Answers 8

5

You could use a regex and replace

^..(\d\d)H(\d\d)M(\d\d)S$

with

$1:$2:$3

(I leave the details of the code as an exercise for the reader.)

Sign up to request clarification or add additional context in comments.

Comments

4

you forgot to do

String time = "PT14H01M00S";
String substring = time .substring(2);
substring = substring.replace("H", ":");
substring = substring.replace("M", ":");
substring = substring.replace("S", "");

System.out.println(substring);

so the substring is not getting the new value returned from replace method

ran it in here and got

enter image description here

1 Comment

Very similar to @Paniz answer!!
3

It seems like you are trying to parse and format a JodaTime Period. You should not do it using the String class API. Use appropriate formatter already provided in JodaTime API.

You should build 2 PeriodFormatters using PeriodFormatterBuilder class, one for parsing the given string into a Period, and then second for formatting the Period to the required format:

String periodString = "PT14H01M00S";

PeriodFormatter parser = new PeriodFormatterBuilder()
        .appendLiteral("PT")
        .appendHours().appendSuffix("H")
        .appendMinutes().appendSuffix("M")
        .appendSeconds().appendSuffix("S")
        .toFormatter();

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .minimumPrintedDigits(2)
        .printZeroAlways()
        .appendHours()
        .appendSeparator(":")
        .appendMinutes()
        .appendSeparator(":")
        .appendSeconds()
        .toFormatter();

System.out.println(formatter.print(parser.parsePeriod(periodString)));

Output:

14:01:00

Comments

2

use this code instead :

 String time = "PT14H01M00S";
 String substring = time .substring(2);
 substring = substring.replace("H", ":");
 substring = substring.replace("M", ":");
 substring = substring.replace("S", "");

Comments

2

the replace method doesn't change the string it was called upon, it returns a new string.

So what you want is:

String time = "PT14H01M00S";
String substring = time .substring(2);
substring = substring.replace("H", ":");
substring = substring.replace("M", ":");
substring = substring.replace("S", "");

Comments

2

I recommend the use of regular expressions to do this kind of tasks. They are very powerful to search and replace patterns of text.

Comments

2

I thinks this should work

String time = "PT14H01M00S".substring(2, time.length - 1).replace(/H|M/, ":");

Comments

2

use this code instead

 String substring = time .substring(2);
 substring = substring.replaceAll("[HM]",":").replace("S", "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.