gpt4 book ai didi

java - 我无法将此字符串转换为日期

转载 作者:行者123 更新时间:2023-12-02 01:53:48 24 4
gpt4 key购买 nike

我想将“Thu Jan 18 00:00:00 CET 2018”转换为“2018-01-18”-> yyyy-mm-dd。

但我收到错误 -> java.text.ParseException:无法解析的日期:“Thu Jan 18 00:00:00 CET 2018”。

switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:

stringValue = String.valueOf(cell.getDateCellValue());
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(stringValue);
System.out.println(date);
break;
case Cell.CELL_TYPE_STRING:...
break;
}

也许这就是为什么我有 cell.getCellType()Cell.CELL_TYPE_NUMERIC已弃用?

最佳答案

似乎您的Locale不是这种格式,请尝试Locale.US,并且您需要使用x作为时区,请尝试:

String input = "Thu Jan 18 00:00:00 CET 2018";

DateFormat parser = new SimpleDateFormat("E MMM dd HH:mm:ss x yyyy", Locale.US);
Date date = parser.parse(input); // parse String to Date

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.format(date)); // format Date to String

或者您可以使用ZonedDateTime从java8开始:

String input = "Thu Jan 18 00:00:00 CET 2018";

ZonedDateTime zonedDateTime = ZonedDateTime.parse(input,
DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.US));

System.out.println(DateTimeFormatter.ISO_DATE.format(zonedDateTime.toLocalDate()));

关于java - 我无法将此字符串转换为日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52593859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com