项目运行时不会创建新日期

发布于 2025-01-10 21:54:23 字数 4930 浏览 0 评论 0原文

项目运行时,数据从数据库上传,然后形成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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2025-01-17 21:54:23

由于 spring bean 是一个单例,fileDate2fileDate3 将使用该 bean 进行初始化,并且只初始化一次。将其移至您的 saveExcel 中。

@Component
public class SaveProviders {

    @Autowired
    private ProviderService providerService;

    private static final DateFormat mounthFormat = new SimpleDateFormat("MMMM");
    private static final DateFormat dayMounthFormat= new SimpleDateFormat("dd.MM");

    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 {

    String fileDate2 = mounthFormat.format(new Date());
    String fileDate3 = dayMounthFormat.format(new Date());

        Workbook workbook = new XSSFWorkbook();

顺便说一句:停止使用过时的 SimpleDateFormatjava.util.Date 类。使用现代 java.time.* API

As a spring bean is a singleton fileDate2 and fileDate3 will be initialized with the bean, exactly one time. move it into your saveExcel.

@Component
public class SaveProviders {

    @Autowired
    private ProviderService providerService;

    private static final DateFormat mounthFormat = new SimpleDateFormat("MMMM");
    private static final DateFormat dayMounthFormat= new SimpleDateFormat("dd.MM");

    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 {

    String fileDate2 = mounthFormat.format(new Date());
    String fileDate3 = dayMounthFormat.format(new Date());

        Workbook workbook = new XSSFWorkbook();

BTW: Stop using the outdated SimpleDateFormat and java.util.Date class. Use the modern java.time.* API

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文