I need a code which should get Date from Excel as YYYY-MM-DD
.
It is displaying the date as 31-Dec-2050 which was originally saved in sheet as 2050-12-31 Format.
System.out.println("<" + sheet.getRow(i).getCell(24) + ">");
should get Date from Excel as 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. 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. The LocalDate
class represents a date-only value without time-of-day and without time zone or offset-from-UTC .
Define a formatting pattern to match your textual input.
Pass a Locale
to determine:
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 .
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) + ">");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.