Reputation: 473
I am using Apache POI to read in an excel dataset and JPA to persist into the database. This is working fine except for the fact i need to get a set of date/time values correctly into the database using the same method (read in with apache poi, then persist into the database). At the moment i am using SimpleDateFormat which is not of any use really. We need to query this column so i need to have to use mySQL dates ideally in the database.
Dataset example of time/date column
21/10/2012 13:20:00
Java
Sheet sheet1 = wb.getSheetAt(0);
String dateTimeFormat = "dd/MM/yyyy HH:mm:ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(dateTimeFormat);
for (int i = 1; i <= sheet1 .getLastRowNum(); i++) {
row = sheet1 .getRow(i);
String sheet1Id = i+"";
double excelDateTime = row.getCell(0).getNumericCellValue();
Date javaDateTime=HSSFDateUtil.getJavaDate(excelDateTime,false);
String dateTime = dateFormat.format(javaDateTime);
Table1 sheet1=new Table1 (sheet1Id ,dateTime)
addToDatabase(table1);
}
Any solutions to this and any advice as to the best way to create this column in the mySQL database aswell(The column is a varchar at present)?
Cheers
Upvotes: 1
Views: 661
Reputation: 421
When using JPA/JDBC, a java.util.Date corresponds to a DATETIME column (or TIMESTAMP, or DATE, see the MySQL-manual to learn about their subtle differences). Since you already get a Date object directly from HSSFDateUtil
, you don't need any further conversion.
PS: Why don't you use HSSFCell.getDateCellValue()
directly?
Upvotes: 2