从字符串java中提取日期 [英] Extract date from String java

查看:962
本文介绍了从字符串java中提取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带多个日期的字符串,例如:

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]

我如何阅读此字符串并提取每个日期?我正在使用 SimpleDateFormat 类创建正则表达式。

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刚做完,就得到了第一个,它更改了格式和时区:

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)) ;

输出:2012年7月20日星期五06:11:36 BST

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

推荐答案

您可以尝试:

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

这篇关于从字符串java中提取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆