【问题标题】:How to extract a date in a specific format from URL using Selenium?如何使用 Selenium 从 URL 中提取特定格式的日期?
【发布时间】:2020-11-29 13:25:03
【问题描述】:
使用:
String URL = driver.getCurrentUrl();
我得到这样的东西:
firstname=&lastname=&sex=Male&exp=1&datepicker=11%2F30%2F2020&photo=&continents=asia&submit=
我必须提取日期并以这种格式打印它:yyyy-mm-dd,即得到2020-11-30。有谁有想法吗?
谢谢!
【问题讨论】:
标签:
java
selenium
date-format
【解决方案1】:
我将假设 getCurrentUrl() 返回完整的 url 而不仅仅是查询字符串。
在高层次上,我将过程视为
-
Url decode字符串
- 将查询字符串解析为地图
- 将日期选择器值从 mm-dd-yyyy 转换为 yyyy-mm-dd,即 iso8601
步骤 1 和 2 可以通过 OkHttp 等库完成
final HttpUrl currentUrl = HttpUrl.parse(driver.getCurrentUrl());
if (currentUrl != null) {
final String inputDate = currentUrl.queryParameter("datepicker");
}
如果你不想使用/那个库there are some zero dependency options in this other question。
第 3 步有一些选项,具体取决于您对此的需求。
- 直接字符串操作。
- 使用库或内置日期实用程序。
如果这无关紧要,字符串操作将是最简单的。只需使用 yyyy、'-' 和 mm-dd 创建一个新字符串。
缺点是它不提供任何验证日期的方法。
这是一个库选项
SimpleDateFormat parseFormatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = parseFormatter.parse(inputDate);
SimpleDateFormat outputFormatter = new SimpleDateFormat("yyyy-MM-dd");
String date = outputFormatter.format(date);
System.out.println(date);
【解决方案2】:
- 您可以使用给定的 URL 字符串创建
Map<String, String> paramMap。
- 处理 URL 字符串并将参数名称作为键和参数值作为值存储到此
Map 中。
- 从此
Map 中获取键值datepicker 并将其解析为日期对象并将日期对象格式化为所需格式的字符串。由于java.util 的日期时间API 和它们的格式化API,SimpleDateFormat 已经过时并且容易出错,我建议你使用modern date-time API。通过 Trail: Date Time 了解有关现代日期时间 API 的更多信息。
我已经编写了同时使用现代 java.time 日期时间 API 和旧版 java.util 日期时间 API 的解决方案。
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException, ParseException {
String strUrl = "https://www.google.com?firstname=&lastname=&sex=Male&exp=1&datepicker=11%2F30%2F2020&photo=&continents=asia";
Map<String, String> map = getParams(strUrl);
String strDate = map.get("datepicker");
// Using Java-8
System.out.println(LocalDate.parse(strDate, DateTimeFormatter.ofPattern("MM/dd/uuuu")));
// Using legacy API
DateFormat sdfInput = new SimpleDateFormat("MM/dd/yyyy");
DateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdfOutput.format(sdfInput.parse(strDate)));
}
public static Map<String, String> getParams(String strUrl) throws UnsupportedEncodingException {
Map<String, String> paramMap = new HashMap<String, String>();
String[] parts = URLDecoder.decode(strUrl, StandardCharsets.UTF_8.name()).split("\\?");
if (parts.length == 2) {
String[] params = parts[1].split("&");
for (String param : params) {
String[] nameVal = param.split("=");
if (nameVal.length == 2) {
String name = nameVal[0];
String value = nameVal[1];
paramMap.put(name, value);
}
}
}
return paramMap;
}
}
输出:
2020-11-30
2020-11-30