[英]parsing and formatting Date and Time in Java
on one site which i want to grab, i have a format date and time like this: 在我想要抓住的一个网站上,我有这样的格式日期和时间:
<div id="date">11 october 2010 <span>|</span> 21:43</div>
at first i want to split the time and date in two variables, and after this push it to my table in my mysql. 起初我想在两个变量中分割时间和日期,然后在我的mysql中将它推送到我的表中。
as well as i need to change the format date in smth like this: 2013-05-10
以及我需要像这样更改格式日期: 2013-05-10
now i have only this: 现在我只有这个:
String date = (driver.findElement(By.id("date"))).getText();
how can i split and change format of this? 我该如何拆分和更改此格式?
I suppose the date string contains something like "11 october 2010 | 21:43". 我想日期字符串包含“2010年10月11日| 21:43”之类的内容。 You can use SimpleDateFormat for this. 您可以使用SimpleDateFormat 。 In your case this should work: 在你的情况下,这应该工作:
SimpleDateFormat simpleDateFormatIn =
new SimpleDateFormat("dd MMMM yyyy '|' HH:mm", Locale.ENGLISH);
SimpleDateFormat simpleDateFormatOut =
new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String dateStr = (driver.findElement(By.id("date"))).getText();
Date date = simpleDateFormatIn.parse(dateStr);
System.out.println(simpleDateFormatOut.format(date));
A nice little blog about this can be found here : 关于这个的一个不错的小博客可以在这里找到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.