Home > OS >  New dates are not created when the project is running
New dates are not created when the project is running

Time:03-03

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();
    }
}

CodePudding user response:

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

  • Related