如何在sting动态android之前和之后添加00

时间:2014-09-22 09:02:28

标签: android string string-formatting

你好我在做这个任务时非常沮丧任何人请帮助我

我有一个字符串12.03,12:12,12等我希望所有时间都以这种格式00:00:00.00是否有任何方法可以将我的字符串自动转换为此格式,如12.02应写为00:00: 12.02和12:12 = 00:12:12.00和12 = 12:00:00.00 ..实际上我正在这样做的时间

StringTokenizer tokens = new StringTokenizer(get_time, ":.");
        while (tokens.hasMoreElements()) {
            time_list.add(tokens.nextToken());


        }

拆分字符串,但如何将此字符串转换为我想要的格式? 提前谢谢

1 个答案:

答案 0 :(得分:0)

你已经对字符串进行了标记,然后就可以像这样做了

import java.text.SimpleDateFormat;
import java.util.*;
public class DateInAndroid {

    public static void main(String[] args) {

        Calendar gregoianDate = new GregorianCalendar();

        SimpleDateFormat sdf = new SimpleDateFormat("KK:mm:ss:SS");     
        String get_time = "15.03";
        List<String> time_list = new ArrayList<String>();
        StringTokenizer tokens = new StringTokenizer(get_time, ":.");
        while (tokens.hasMoreElements()) {
            time_list.add(tokens.nextToken());           

        }
        if(get_time.contains(".")){
            gregoianDate.set(Calendar.HOUR, 0);
            gregoianDate.set(Calendar.MINUTE, 0);
            gregoianDate.set(Calendar.SECOND,Integer.parseInt(time_list.get(0)));
            gregoianDate.set(Calendar.MILLISECOND, Integer.parseInt(time_list.get(1)));
        }
        else if(get_time.contains(":"))
        {
            gregoianDate.set(Calendar.HOUR, 0);
            gregoianDate.set(Calendar.MINUTE,Integer.parseInt(time_list.get(0)));
            gregoianDate.set(Calendar.SECOND, Integer.parseInt(time_list.get(1)));
            gregoianDate.set(Calendar.MILLISECOND, 0);
        }
        else
        {
            gregoianDate.set(Calendar.HOUR,Integer.parseInt(time_list.get(0)));
            gregoianDate.set(Calendar.MINUTE, 0);
            gregoianDate.set(Calendar.SECOND, 0);
            gregoianDate.set(Calendar.MILLISECOND, 0);
        }
        System.out.println(sdf.format(gregoianDate.getTime()));
    }

}

输出:00:00:15:03


来源:链接1 click here

链接2 click Here