[英]I need a code which should get Date from Excel as YYYY-MM-DD format using java
I need a code which should get Date from Excel as YYYY-MM-DD
. 我需要一个应从Excel获取Date为
YYYY-MM-DD
。
It is displaying the date as 31-Dec-2050 which was originally saved in sheet as 2050-12-31 Format. 它显示的日期为2050年12月31日,该日期最初以2050-12-31格式保存在工作表中。
System.out.println("<" + sheet.getRow(i).getCell(24) + ">");
should get Date from Excel as YYYY-MM-DD
. 应该从Excel获得Date为
YYYY-MM-DD
。
LocalDate.parse(
"31-Dec-2050" ,
DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US )
)
.toString()
Your Question is unclear as you do not explain what library you might be using to read Excel files, nor do you explain what sheet.getRow(i).getCell(24)
is doing. 您的问题尚不清楚,因为您没有解释可能使用的库来读取Excel文件,也没有解释
sheet.getRow(i).getCell(24)
在做什么。 So I can only address how to parse a date string, and not give a potentially better, deeper answer. 因此,我只能解决如何解析日期字符串的问题,而不能给出可能更好,更深入的答案。
If you are given the string 31-Dec-2050
you can parse that as a LocalDate
in Java. 如果为您提供字符串
31-Dec-2050
,则可以将其解析为Java中的LocalDate
。 The LocalDate
class represents a date-only value without time-of-day and without time zone or offset-from-UTC . LocalDate
类表示仅包含日期的值,没有日期, 时间段或offset-from-UTC 。
Define a formatting pattern to match your textual input. 定义格式模式以匹配您的文本输入。
Pass a Locale
to determine: 通过
Locale
确定:
Ex: 例如:
String input = "31-Dec-2050" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
See this code run live at IdeOne.com . 看到此代码在IdeOne.com上实时运行 。
ld.toString(): 2050-12-31
ld.toString():2050-12-31
Import: 进口:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
Then in your class: 然后在您的课程中:
String cv = sheet.getRow(i).getCell(24).getStringCellValue();
DateTimeFormatter dt = DateTimeFormatter.ofPattern( "uuuu-MM-dd");
System.out.println("<" + LocalDate.parse(cv).format(dt) + ">");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.