解析特定字符串到日期

Eltomon:

纵观通过Selenium读取整个表,并将结果写入到一个Excel文件,我目前面临的一个问题格式化/解析日期StringDate对象。我想存档的格式如下:

dd-mm-yyyy

这是从表中这个样子的检索到的dateString

16 APR 2020

我试图用SimpleDateFormat格式化,但我得到一个ParseException

java.text.ParseException: Unparseable date: "16 Apr 2020"
deHaar:

只是为了记录:这是如何解析和格式化日期String与现在java.time(从Java 8个可用):

public static void main(String[] args) {
    // the date String
    String d = "16 Apr 2020";
    // which is parsed to a LocalDate using a formatter with a suitable pattern
    LocalDate ld = LocalDate.parse(d, DateTimeFormatter.ofPattern("dd MMM yyyy"));
    // and which is then printed in a different format using a formatter with a differen pattern
    System.out.println(ld.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
}

这样做的输出

16-04-2020

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章