Extract date from String java

家住魔仙堡 提交于 2019-12-02 06:37:50

问题


I have a String with several dates, for example:

[20-Jul-2012 5:11:36,670 UTC PM, 20-Jul-2012 5:11:36,683 UTC PM]

How do I read this string and extract each date? I'm using the SimpleDateFormat class to create a regex.

SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a");

I've tried :

I've just did, to get the first one and it changes the format and the timezone:

ParsePosition parsePos = new ParsePosition(1);
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a");
System.out.println(format2.parse(entry.getValue().toString(), parsePos)) ;

Output : Fri Jul 20 06:11:36 BST 2012


回答1:


You can try:

 parsePos = new ParsePosition(1);
 while((date = format2.parse(yourString, parsePos)!=null){
      //use date
 }


来源:https://stackoverflow.com/questions/11599591/extract-date-from-string-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!