I changed the csv format to:

name,purchase,date
TEST,TEST,2018-09-16T08:00:00
TEST,TEST,2018-09-16T08:00:00

I modified the class that binds to the csv to look like this:

public class CsvRecords {
  @CsvBindByName
  private String name;
  @CsvBindByName
  private String purchase;
  @CsvBindByName
  private String date;

  // get and setters left out for brevity pls comment if needed
}

The POJO class for the data in db:

public class Records {
  private String name;
  private String purchase;
  private Timestamp date;

  // get and setters left out for brevity pls comment if needed
}

When uploading in the controller class I then convert the string to LocalDateTime and then again to Timestamp like this:

@PostMapping("/upload-csv-file")
    public String uploadCSVFile(@RequestParam("file") MultipartFile file, Model model) {

        // validate file
        if (file.isEmpty()) {
            model.addAttribute("message", "Please select a CSV file to upload.");
            model.addAttribute("status", false);
        } else {

            // parse CSV file to create a list of `User` objects
            try (Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {

                // create csv bean reader
                CsvToBean<Records> csvToBean = new CsvToBeanBuilder(reader)
                        .withType(Records.class)
                        .withIgnoreLeadingWhiteSpace(true)
                        .build();

                // convert `CsvToBean` object to list of records
                List<Records> records = csvToBean.parse();

                // save users in DB?
                for(int i = 0; i<records.size(); i++){
                   LocalDateTime localDateTime = LocalDateTime.parse(records.get(i).getDate);
                   Timestamp timestamp = Timestamp.valueOf(localDateTime);
                   Records rec = new Records(records.get(i).getName(), records.get(i).getPurchase(), timestamp)
                   firebaseServices.saveDetails(rec);
                 }
            } catch (Exception ex) {
                model.addAttribute("message", "An error occurred while processing the CSV file.");
                model.addAttribute("status", false);
            }
        }

        return "file-upload-status";
    }

For details on the implementation of the firebaseServices class (saveDetails method) I used this tutorial

Answer from Alfie Danger on Stack Overflow
🌐
Medium
medium.com › @mohamedhedi.aissi › spring-boot-csv-service-using-opencsv-5afd5c66c125
Populating DataBase in Spring Boot using CSV files and OpenCsv | by Mohamed Hedi Aissi | Medium
June 17, 2024 - <!-- Adding the dependancy for OpenCsv with version 5.7.1 --> implementation 'com.opencsv:opencsv:5.7.1' For this tutorial , our goal is to import infos to database at the start of the springBoot application to populate Country table .
🌐
GitHub
github.com › knowledgefactory4u › springboot-opencsv
GitHub - knowledgefactory4u/springboot-opencsv: Spring Boot + OpenCSV Export Data to CSV Example
Spring Boot + OpenCSV Export Data to CSV Example . Contribute to knowledgefactory4u/springboot-opencsv development by creating an account on GitHub.
Author   knowledgefactory4u
🌐
Baeldung
baeldung.com › home › java › java io › introduction to opencsv
Introduction to OpenCSV | Baeldung
May 11, 2024 - Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
🌐
Medium
medium.com › @priyaroul99 › writing-csv-files-using-opencsv-and-spring-boot-02e141dcba10
Writing CSV files using OpenCSV and Spring Boot | by Priya Roul | Medium
June 23, 2025 - Writing CSV files using OpenCSV and Spring Boot In this article we will implement a CSV download feature using OpenCSV library and Spring Boot using StreamingResponseBody. CSV stands for …
Top answer
1 of 3
2

I changed the csv format to:

name,purchase,date
TEST,TEST,2018-09-16T08:00:00
TEST,TEST,2018-09-16T08:00:00

I modified the class that binds to the csv to look like this:

public class CsvRecords {
  @CsvBindByName
  private String name;
  @CsvBindByName
  private String purchase;
  @CsvBindByName
  private String date;

  // get and setters left out for brevity pls comment if needed
}

The POJO class for the data in db:

public class Records {
  private String name;
  private String purchase;
  private Timestamp date;

  // get and setters left out for brevity pls comment if needed
}

When uploading in the controller class I then convert the string to LocalDateTime and then again to Timestamp like this:

@PostMapping("/upload-csv-file")
    public String uploadCSVFile(@RequestParam("file") MultipartFile file, Model model) {

        // validate file
        if (file.isEmpty()) {
            model.addAttribute("message", "Please select a CSV file to upload.");
            model.addAttribute("status", false);
        } else {

            // parse CSV file to create a list of `User` objects
            try (Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {

                // create csv bean reader
                CsvToBean<Records> csvToBean = new CsvToBeanBuilder(reader)
                        .withType(Records.class)
                        .withIgnoreLeadingWhiteSpace(true)
                        .build();

                // convert `CsvToBean` object to list of records
                List<Records> records = csvToBean.parse();

                // save users in DB?
                for(int i = 0; i<records.size(); i++){
                   LocalDateTime localDateTime = LocalDateTime.parse(records.get(i).getDate);
                   Timestamp timestamp = Timestamp.valueOf(localDateTime);
                   Records rec = new Records(records.get(i).getName(), records.get(i).getPurchase(), timestamp)
                   firebaseServices.saveDetails(rec);
                 }
            } catch (Exception ex) {
                model.addAttribute("message", "An error occurred while processing the CSV file.");
                model.addAttribute("status", false);
            }
        }

        return "file-upload-status";
    }

For details on the implementation of the firebaseServices class (saveDetails method) I used this tutorial

2 of 3
0

Could you have a try in this case that replaces the Timestamp to Date? like follow this:

@CsvBindByName private Date date;

🌐
ZetCode
zetcode.com › springboot › csv
Spring Boot CSV - serving CSV data in a Spring Boot application
August 2, 2023 - The data is stored in a plain text file. It is very popular as import and export format used in spreadsheets and databases. Opencsv is an open source, simple CSV parser library for Java. Our application is a Spring Boot RESTful application which returns data from an H2 database in a CSV format.
🌐
GitHub
github.com › jusexton › spring-opencsv-example › blob › master › build.gradle
spring-opencsv-example/build.gradle at master · jusexton/spring-opencsv-example
OpenCsv example using Spring Boot. Contribute to jusexton/spring-opencsv-example development by creating an account on GitHub.
Author   jusexton
🌐
HugeDomains
knowledgefactory.net › 2020 › 12 › spring-boot-export-data-to-csv-example.html
Spring Boot + OpenCSV Export Data to CSV Example
December 25, 2020 - Get this domain name before someone else does. Quick and painless shopping. Affordable payment options available.
Find elsewhere
🌐
Codersee
blog.codersee.com › home › spring boot csv export – opencsv
Spring Boot CSV export - OpenCSV
April 16, 2025 - implementation("com.opencsv:opencsv:5.2") implementation("org.springframework.boot:spring-boot-starter-web")
🌐
Attacomsian
attacomsian.com › blog › spring-boot-upload-parse-csv-file
How to upload and parse a CSV file using Spring Boot
September 24, 2022 - OpenCSV provides this annotation to specify a binding between a column name of the CSV input and a field in a bean. You can only use the @CsvBindByName annotation if the CSV file has a header.
🌐
SpringHow
springhow.com › 🏠 › java › opencsv for reading and writing csv file in java
OpenCSV for Reading and Writing CSV file in Java | SpringHow
June 29, 2021 - So far, we learned many ways to read and write CSV files using OpenCSV library in Java with examples. All of the examples from above are available at our GitHub repository. Apache Commons CSV to Read and Write CSV files in Java · Using Java to write Data into CSV files · Handling Lists in Thymeleaf view · Spring Boot Custom Health Indicators ·
🌐
Attacomsian
attacomsian.com › blog › export-download-data-csv-file-spring-boot
How to export & download data as a CSV file in Spring Boot
September 24, 2022 - Data export (JSON, CSV, PDF, etc.) ... support for creating and parsing CSV files, we shall use OpenCSV, a 3rd-party library for parsing and creating CSV files....
🌐
Wordpress
simplifyingtechcode.wordpress.com › 2022 › 11 › 06 › how-to-export-data-to-csv-using-springboot-opencsv
How to Export Data to CSV Using SpringBoot & OpenCSV – Simplifying Tech and Programming
November 6, 2022 - In this Session, we a re going ... to use Spring JPA and write the response as CSV. OpenCSV is an easy-to-use CSV (comma-separated values) parser library ......
🌐
YouTube
youtube.com › watch
How To Export Data To Csv Using Springboot and OpenCSV - YouTube
⭐How To Export Data To Csv Using Springboot OpenCSV⭐In this session we will learn to code CSV export function for an existing Spring Boot application using O...
Published   November 12, 2022
🌐
MojoAuth
mojoauth.com › parse-and-generate-formats › parse-and-generate-csv-with-spring-boot
Parse and Generate CSV with Spring Boot | Parse and Generate Formats
Spring Boot makes generating CSV files straightforward, typically involving converting a collection of Java objects into a CSV format. You can leverage libraries like opencsv's BeanToCsv or Jackson's CsvMapper for this transformation.
🌐
GitHub
github.com › ekim197711 › springboot-opencsv
GitHub - ekim197711/springboot-opencsv: Convert csv-files to and from Java beans with OpenCSV · GitHub
Convert csv-files to and from Java beans with OpenCSV - ekim197711/springboot-opencsv
Forked by 3 users
Languages   HTML 82.8% | JavaScript 6.4% | Java 6.2% | CSS 4.6%
🌐
Ucsb-cs56
ucsb-cs56.github.io › topics › spring_boot_csv
Downloading and Uploading CSV files with Spring Boot
https://grokonez.com/spring-framework/spring-boot/csv-file-download-from-springboot-restapi-opencsv-mysql
🌐
Codersee
blog.codersee.com › home › upload csv files in spring boot rest api with kotlin and opencsv
Upload CSV Files in Spring Boot REST API with Kotlin and OpenCSV
April 16, 2025 - implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.opencsv:opencsv:5.2")