如何从这个字符串中解析出时间戳? [英] How to parse out Timestamp from this String?
本文介绍了如何从这个字符串中解析出时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个示例字符串:
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
所以,从这些字符串中,我需要取出时间戳:
So, from these strings, I need to take out the time stamp:
2010-07-21T08:52:05.222 or
2010-07-22T07:02:05.370
基本上价值b / w d ^和Z
Basically values b/w d^ and Z
什么是最好的(最聪明的)这样做的方式? substring(),regex?
What is the best ("smartest") way to do this? substring(), regex?
推荐答案
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
}
取自其他
此外,如果你循环遍历一系列字符串,请确保重用 Pattern p
对象
Also, make sure to reuse the Pattern p
object if you're looping through a series of strings
这篇关于如何从这个字符串中解析出时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文