-4

我想将字符串标记private String requestDate="12/12/2012 02:02:23 AM";

12122012_020223am_

如何使用 stringTokenizer 实现这一点?

问候,

4

3 回答 3

2
requestDate = requestDate.replaceAll("/", "");
requestDate = requestDate.replaceAll(" ", "_");
requestDate = requestDate.replaceAll(":", "");

会给你12122012_020223_AM

然后 requestDate = requestDate.toLowerCase()

会给12122012_020223_am

于 2012-10-05T09:04:45.230 回答
1

使用SimpleDateFormat

Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(requestDate);
String output = new SimpleDateFormat("MMddyyyy_hhmmssa_").format(date);
output = output.toLowerCase();
于 2012-10-05T09:30:22.767 回答
0

如果你想要 StringTokenise 这样的事情应该这样做

@Test
public void test() {
    StringTokenizer stringTokenizer = new StringTokenizer("ab/c/d EF", "/");
    StringBuilder b = new StringBuilder();
    while (stringTokenizer.hasMoreTokens()) {
        StringTokenizer stringTokenizer2 = new StringTokenizer(stringTokenizer.nextToken(), " ");
        while (stringTokenizer2.hasMoreTokens()) {
            b.append(stringTokenizer2.nextToken().toLowerCase());
            b.append("_");
        }
    }
    assertEquals("ab_c_d_ef_", b.toString());
}

StringTokenizer构造函数之一采用字符串和分隔符

于 2012-10-05T09:07:41.120 回答