项目运行时不会创建新日期
项目运行时,数据从数据库上传,然后形成excel文件,并记录上传时的日期。 该项目在本地运行正常。但在服务器上,日期仅在部署时设置。这不应该是这样。必须在生成文件时设置日期。该项目由 Docker 提供支持。我不明白是什么原因。是在我的代码中还是 Docker 设置中? 本地我有Windows。服务器-Linux
@Component
public class SaveProviders {
@Autowired
private ProviderService providerService;
String fileDate2 = new SimpleDateFormat("MMMM").format(new Date());
String fileDate3 = new SimpleDateFormat("dd.MM").format(new Date());
private static final Logger LOGGER = LoggerFactory.getLogger(SaveProviderStartupRunner.class);
public void saveCardsDay() throws IOException {
LocalDate today = LocalDate.now();
String fileDate = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
String fileDate1 = (today.minusDays(1)).format(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
saveExcel(providerService.findAllProviders(), "Report" + fileDate1 + ".xlsx");
}
private void saveExcel(List<Providers> list, String fileName) throws IOException {
Workbook workbook = new XSSFWorkbook();
CreationHelper createHelper = workbook.getCreationHelper();
Sheet sheet = workbook.createSheet("Providers");
sheet.autoSizeColumn(0);
sheet.setColumnWidth(1, 2500);
sheet.setColumnWidth(2, 2500);
sheet.setColumnWidth(3, 3000);
sheet.setColumnWidth(4, 12000);
sheet.autoSizeColumn(5);
sheet.autoSizeColumn(6);
sheet.setColumnWidth(7, 5000);
sheet.setColumnWidth(8, 12000);
sheet.setColumnWidth(9, 10000);
Row header = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFFont font = ((XSSFWorkbook) workbook).createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 10);
font.setBold(true);
headerStyle.setFont(font);
Cell headerCell = header.createCell(0);
headerCell.setCellValue("id");
headerCell.setCellStyle(headerStyle);
/.../
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
CellStyle style1 = workbook.createCellStyle();
style1.setDataFormat(
createHelper.createDataFormat().getFormat("d/m/yyyy"));
CellStyle style2 = workbook.createCellStyle();
style2.setDataFormat(
createHelper.createDataFormat().getFormat("h:mm:ss"));
int ix_row=2;
for (Providers providers : list) {
Row row = sheet.createRow(ix_row);
Cell cell = row.createCell(0);
cell.setCellValue(providers.getId());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(providers.getCreated_dt());
cell.setCellStyle(style1);
cell = row.createCell(2);
cell.setCellValue(providers.getCreated_dt());
cell.setCellStyle(style2);
cell = row.createCell(3);
cell.setCellValue(providers.getUsername());
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue(providers.getName());
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue(providers.getAmount());
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue(providers.getStatus());
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue(providers.getAccount());
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue(providers.getExternal_id());
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue(providers.getExternal_status());
cell.setCellStyle(style);
ix_row++;
}
File pathAsFile = new File("/Reports/Partner_reports_" + fileDate2);
try {
pathAsFile.mkdirs();
LOGGER.info("Folder partners created");
} catch (Exception e) {
LOGGER.error("Folder partners not created cause" + e.getMessage());
}
File pathAsFile1 = new File("/Reports/Partner_reports_" + fileDate2 + "/" + fileDate3);
try {
pathAsFile1.mkdirs();
LOGGER.info("Daily folder partners created");
} catch (Exception e) {
LOGGER.error("Daily folder partners not created cause" + e.getMessage());
}
File outputFile = new File(pathAsFile1, fileName);
FileOutputStream outputStream = new FileOutputStream(outputFile);
workbook.write(outputStream);
workbook.close();
}
}
When the project is running, data is uploaded from the database, which is then formed into excel files and recorded with dates at the time of uploading.
Locally the project works correctly. But on the server, dates are set only at the time of deployment. This should not be. Dates must be set at the time the file was generated. The project is powered by Docker. I can't figure out what is the reason. Is it in my code or is it the Docker settings.
Locally I have Windows. Server - Linux
@Component
public class SaveProviders {
@Autowired
private ProviderService providerService;
String fileDate2 = new SimpleDateFormat("MMMM").format(new Date());
String fileDate3 = new SimpleDateFormat("dd.MM").format(new Date());
private static final Logger LOGGER = LoggerFactory.getLogger(SaveProviderStartupRunner.class);
public void saveCardsDay() throws IOException {
LocalDate today = LocalDate.now();
String fileDate = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
String fileDate1 = (today.minusDays(1)).format(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
saveExcel(providerService.findAllProviders(), "Report" + fileDate1 + ".xlsx");
}
private void saveExcel(List<Providers> list, String fileName) throws IOException {
Workbook workbook = new XSSFWorkbook();
CreationHelper createHelper = workbook.getCreationHelper();
Sheet sheet = workbook.createSheet("Providers");
sheet.autoSizeColumn(0);
sheet.setColumnWidth(1, 2500);
sheet.setColumnWidth(2, 2500);
sheet.setColumnWidth(3, 3000);
sheet.setColumnWidth(4, 12000);
sheet.autoSizeColumn(5);
sheet.autoSizeColumn(6);
sheet.setColumnWidth(7, 5000);
sheet.setColumnWidth(8, 12000);
sheet.setColumnWidth(9, 10000);
Row header = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFFont font = ((XSSFWorkbook) workbook).createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 10);
font.setBold(true);
headerStyle.setFont(font);
Cell headerCell = header.createCell(0);
headerCell.setCellValue("id");
headerCell.setCellStyle(headerStyle);
/.../
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
CellStyle style1 = workbook.createCellStyle();
style1.setDataFormat(
createHelper.createDataFormat().getFormat("d/m/yyyy"));
CellStyle style2 = workbook.createCellStyle();
style2.setDataFormat(
createHelper.createDataFormat().getFormat("h:mm:ss"));
int ix_row=2;
for (Providers providers : list) {
Row row = sheet.createRow(ix_row);
Cell cell = row.createCell(0);
cell.setCellValue(providers.getId());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(providers.getCreated_dt());
cell.setCellStyle(style1);
cell = row.createCell(2);
cell.setCellValue(providers.getCreated_dt());
cell.setCellStyle(style2);
cell = row.createCell(3);
cell.setCellValue(providers.getUsername());
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue(providers.getName());
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue(providers.getAmount());
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue(providers.getStatus());
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue(providers.getAccount());
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue(providers.getExternal_id());
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue(providers.getExternal_status());
cell.setCellStyle(style);
ix_row++;
}
File pathAsFile = new File("/Reports/Partner_reports_" + fileDate2);
try {
pathAsFile.mkdirs();
LOGGER.info("Folder partners created");
} catch (Exception e) {
LOGGER.error("Folder partners not created cause" + e.getMessage());
}
File pathAsFile1 = new File("/Reports/Partner_reports_" + fileDate2 + "/" + fileDate3);
try {
pathAsFile1.mkdirs();
LOGGER.info("Daily folder partners created");
} catch (Exception e) {
LOGGER.error("Daily folder partners not created cause" + e.getMessage());
}
File outputFile = new File(pathAsFile1, fileName);
FileOutputStream outputStream = new FileOutputStream(outputFile);
workbook.write(outputStream);
workbook.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 spring bean 是一个单例,
fileDate2
和fileDate3
将使用该 bean 进行初始化,并且只初始化一次。将其移至您的 saveExcel 中。顺便说一句:停止使用过时的
SimpleDateFormat
和java.util.Date
类。使用现代java.time.*
APIAs a spring bean is a singleton
fileDate2
andfileDate3
will be initialized with the bean, exactly one time. move it into your saveExcel.BTW: Stop using the outdated
SimpleDateFormat
andjava.util.Date
class. Use the modernjava.time.*
API