-2

I want to write a java method that takes a string in input and outputs another string following this rule:

input        output
"123456"     "12:34:56"
"23456"      "02:34:56"
"3456"       "00:34:56"
"456"        "00:04:56"
"6"          "00:00:06"

Any help would be appreciated. Thanks.

Dici
  • 25,226
  • 7
  • 41
  • 82
user3415827
  • 61
  • 1
  • 4
  • wanna post at least some pseudocode? – ControlAltDel Oct 15 '14 at 15:04
  • 1
    What you have tried? Could you post code that isnt working? – Gerret Oct 15 '14 at 15:04
  • There are two simple steps: 1) Add 0 at the beginning to have a string with a length equal to 6 2) Add ':' symbols inside the string – Sergey Malyutin Oct 15 '14 at 15:05
  • write down an algorithm for the problem. First figure out what exactly needs to happen to a size 1 string to reach the desired output, then determine the steps to achieve the output with your largest string. What are the steps involved? Write them out on paper and pencil. Then list the steps in order. You have your algorithm. Convert to java. – scrappedcola Oct 15 '14 at 15:08
  • possible duplicate of [Convert Java string to Time, NOT Date](http://stackoverflow.com/questions/18604408/convert-java-string-to-time-not-date) – Basil Bourque Oct 15 '14 at 16:55

2 Answers2

0

I would advice to use DateFormat as below. This will take care of all the burdens of conversion.

    DateFormat formatFrom = new SimpleDateFormat("HHmmss");
    DateFormat formatTo = new SimpleDateFormat("HH:mm:ss");
    String origTimeString = "456";
    String newDateString = null;
    try {
        String formattedString = 
                        String.format("%06d", Integer.parseInt(origTimeString));
        Date date = formatFrom.parse(formattedString);
        newDateString = formatTo.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println("Updated string : " + newDateString);
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

Copy this method and use it.

1) If string length is more than 6, it's going to return "ERROR".

2) First fixes the String with '0'

3) Second fixes the String with ':'

4) StringBuilder is used for concat. Avoid using '+' operator for concat.

5) Method 'getDate' is static because of 'main' method is static, too.

public static String getDate(String variable){

    StringBuilder aux= new StringBuilder();
    StringBuilder string= new StringBuilder();
    int length = variable.length();


    if(length>6 || length<=0){
        return "ERROR";
    }else{

        //first to fill blanks with 0
        for(int i=0;i<6-length;i++){
            aux.append("0");
        }
        variable = aux.append(variable).toString();


        //second to put :
        for(int i=0;i<6;i++){

           if(i%2==0 && i!=0){  
               string.append(":");
           }
           string.append(variable.charAt(i));
        }
        return string.toString();
    }
}

public static void main(String[] args) {
    System.out.print(getDate("464"));
}