How to solve the problem that the date type of java poi excel import java has changed

java poi excel table imports java date type has changed how to solve

in the Excel table

clipboard.png

java

clipboard.png

Why did this date change
Code:
package data_sorting;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test5 {

static List<String> hbvaluelist = null;
public static void main(String[] args) {
    JFrame frame = new JFrame("");
    JButton button = new JButton("");
    JButton button2 = new JButton("");
    final JTextField tf = new JTextField();
    final JTextField tf2 = new JTextField();
    final JTextField tf3 = new JTextField();
    final JTextArea tf4 = new JTextArea();
    final JTextField tf5 = new JTextField("yyyy/MM/dd");
    final JTextField tf6 = new JTextField();
    JScrollPane jsp = new JScrollPane(tf4);
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JLabel jl1 = new JLabel(":");
    JLabel jl2 = new JLabel(":");
    JLabel jl3 = new JLabel(":");
    JLabel jl4 = new JLabel(":");
    JLabel jl5 = new JLabel("*:yyyy :MM :dd :2000/10/20 :yyyy/MM/dd");
    JLabel jl6 = new JLabel(":");
    JLabel jl7 = new JLabel("* ");
    frame.setLayout(null);
    frame.setSize(800, 700);
    jl1.setBounds(110, 0, 160, 30);
    jl2.setBounds(20, 35, 50, 30);
    jl3.setBounds(20, 70, 50, 30);
    jl4.setBounds(10, 105, 90, 30);
    jl5.setBounds(200, 105, 400, 30);
    jl6.setBounds(150, 70, 50, 30);
    jl7.setBounds(250, 70, 200, 30);
    button.setBounds(0, 0, 100, 30);
    button2.setBounds(0, 140, 100, 30);
    tf.setBounds(160, 0, 500, 30);
    tf2.setBounds(90, 35, 50, 30);
    tf3.setBounds(90, 70, 50, 30);
    tf5.setBounds(100, 105, 80, 30);
    jsp.setBounds(10, 175, 770, 400);
    tf6.setBounds(200, 70, 50, 30);
    frame.add(button);
    frame.add(button2);
    frame.setVisible(true);
    frame.add(tf);
    frame.add(tf2);
    frame.add(tf3);
    frame.add(jsp);
    frame.add(tf5);
    frame.add(tf6);
    frame.add(jl1);
    frame.add(jl2);
    frame.add(jl3);
    frame.add(jl4);
    frame.add(jl5);
    frame.add(jl6);
    frame.add(jl7);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // 
            JFileChooser chooser = new JFileChooser(); // 
            chooser.setMultiSelectionEnabled(false); // 
            int returnVal = chooser.showOpenDialog(button); // 
            
            if (returnVal == JFileChooser.APPROVE_OPTION) { // 

                String filepath = chooser.getSelectedFile().getAbsolutePath(); // 
                tf.setText(filepath);
                button2.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        tf4.setText( " ");
                        String url = tf.getText();
                        String sheet = tf2.getText();
                        String num = tf3.getText();
                        String format = tf5.getText();
                        String num2 = tf6.getText();
                        try {
                            List<String> hbdatelist = showExcel(url, sheet, num, format,num2);
                            for (int i = 0; i < hbdatelist.size(); iPP) {
                                
                                tf4.append(hbdatelist.get(i) + "\t" + hbvaluelist.get(i)+"\n");
                            }
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        
                    }
                });
                System.out.println(filepath);
                System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); // 
                
            }
        }
    });
}
public static List<String> showExcel(String url,String sheet,String num,String format,String num2) throws Exception {
    List<String> datev = new ArrayList<String>();
    List<String> value = new ArrayList<String>();
    List<String> newdatevlist = new ArrayList<String>();
    List<String> hbdatelist = new ArrayList<String>();
    hbvaluelist = new ArrayList<String>();
    
    SimpleDateFormat simp = new SimpleDateFormat(format);
    //SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd");
    XSSFWorkbook hssfWorkbook = new XSSFWorkbook(new FileInputStream(new File(url)));
    // 
    int sh = Integer.parseInt(sheet)-1;
    int nu = Integer.parseInt(num)-1;
    int nu2 = Integer.parseInt(num2)-1;
    XSSFSheet sheetAt = hssfWorkbook.getSheetAt(sh);
    if (sheetAt == null) {
        
    }
    String starttime = sheetAt.getRow(nu2).getCell(nu).toString();
    String endtime = sheetAt.getRow(sheetAt.getLastRowNum()).getCell(nu).toString();
    System.err.println(starttime);
    System.err.println(endtime);
    Date startdate = simp.parse(starttime);
    Date enddate = simp.parse(endtime);
    List<Date> dateList = findDates(startdate, enddate);
    for (Date date : dateList) {
        newdatevlist.add(simp.format(date));

    }

    // 
    for (int i = nu2; i < sheetAt.getLastRowNum() + 1; iPP) {
        XSSFRow row = sheetAt.getRow(i);
        if (row == null) {
            continue;
        }
        // 
        XSSFCell cell1 = row.getCell(nu);
        XSSFCell cell2 = row.getCell(nu-8);
        datev.add(cell1.toString().substring(0, 10));
        value.add(cell2.toString());

    }
    int count = 0;
    for (int i = 0; i < newdatevlist.size() - 1; iPP) {
        if (newdatevlist.get(i).equals(datev.get(count))) {
            //hbdatelist.add(newdatevlist.get(i));
            hbdatelist.add(newdatevlist.get(i));
            hbvaluelist.add(value.get(count));
            countPP;
        } else {
            //hbdatelist.add(newdatevlist.get(i));
            hbdatelist.add(newdatevlist.get(i));
            hbvaluelist.add(value.get(count - 1));
        }

    }
    hbdatelist.add(newdatevlist.get(newdatevlist.size() - 1));
    hbvaluelist.add(value.get(value.size() - 1));
    return hbdatelist;
    
}

// JAVA
public static List<Date> findDates(Date dStart, Date dEnd) {
    Calendar cStart = Calendar.getInstance();
    cStart.setTime(dStart);

    List<Date> dateList = new ArrayList<Date>();
    // 
    dateList.add(dStart);
    // 
    while (dEnd.after(cStart.getTime())) {
        // 
        cStart.add(Calendar.DAY_OF_MONTH, 1);
        dateList.add(cStart.getTime());
    }
    return dateList;
}

}

Nov.17,2021

1.excle will format the data in a special format itself when it is saved to verify that you have output the input you think can output a piece of log from the console when writing to excle.
2. The provided code snippet does not output the console statement, you can check your output expression.
3. If you want to export to excle format, you can also refer to the use of', 'delimiter output csv file, do not call the POI package, simple and practical.

Menu