我有这个示例字符串:
Sample string 1:
A^1.1#B^1#I^2#f^0#p^1#d^2010-07-21T08:52:05.222ZKHBDGSLKHFBDSLKFGNIF#%$%^$#^$XLGCREWIGMEWCERG
Sample string 2:
A^1.1#B^1#f^0#p^1#d^2010-07-22T07:02:05.370ZREGHCOIMIYR$#^$#^$#^EWMGCOINNNNNNVVVRFGGYVJ667VTG
因此,我需要从这些字符串中取出时间戳:
2010-07-21T08:52:05.222 or
2010-07-22T07:02:05.370
基本上是黑白值d^和Z
最好的(“最聪明的”)方法是什么?子字符串(),正则表达式?
最佳答案
Pattern p = Pattern.compile("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3})");
//you could also use "d\\^(.*)Z" as your regex patern
Matcher m = p.matcher("your string here");
if (m.find()) {
System.out.println(m.group(1)); //print out the timestamp
}
取自 here
此外,如果您要循环访问一系列字符串,请确保重用 Pattern p
对象
关于java - 如何从这个字符串中解析出时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3302870/