[英]Facing problem while reading date time in java from excel by converting date into String Format then parsing it
Trying to store date time from excel to setter using poi and LocalDateTime and DateTimeFormatter.尝试使用 poi 和 LocalDateTime 和 DateTimeFormatter 将日期时间从 excel 存储到 setter。
DateTimeFormatter format=DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss");
LocalDateTime
dateObj=LocalDateTime.parse(row.getCell(2).getLocalDateTimeCellValue().toString(),format);
dto.setDate(dateObj);
The output is: output 是:
java.time.format.DateTimeParseException: Text '2023-01-22T00:00' could not be parsed at index 2
Please advise me what to do?请告诉我该怎么做? In the excel file the date is stored in 1/22/2023 12:00:00 AM
在 excel 文件中,日期存储在 1/22/2023 12:00:00 AM
You are using DateTimeFormatter
for the wrong purpose.您将
DateTimeFormatter
用于错误的目的。 Instead of formatting a LocalDateTime
object, you are trying to parse a string unnecessarily.您没有格式化
LocalDateTime
object,而是试图不必要地解析字符串。 You need a formatted String
which you can do as follows:您需要一个格式化的
String
,您可以按如下方式进行操作:
LocalDateTime dateObj = row.getCell(2).getLocalDateTimeCellValue();
DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH);
String formatted = dateObj.format(format);
Learn more about the modern Date-Time API from Trail: Date Time .从Trail:Date Time了解有关现代日期时间 API 的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.