简体   繁体   中英

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.

as well as i need to change the format date in smth like this: 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". You can use SimpleDateFormat for this. 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 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM