0

I have successfully imported date column from excel to the java code. However, I am unable to change the date format from 17-Dec-2018 to 2018-12-17. Kindly help.

public  void saveToDatabase(Vector dataHolder) throws ParseException {
          System.out.println(dataHolder);
          for(Iterator iterator = dataHolder.iterator();iterator.hasNext();) {
              List list = (List) iterator.next();
                fullName = list.get(0).toString();
                idNumberString = list.get(1).toString();
                                //idNumber = Integer.parseInt ( idNumberString );
                committee = list.get(2).toString();
                amountString = list.get(3).toString();
                                //amount = Integer.parseInt ( amountString );
                bosaFosa = list.get(4).toString();
                purpose = list.get(5).toString();
                paymentsType = list.get(6).toString();
                supportingDocuments = list.get(7).toString();
                entryDate = list.get(8).toString();
                }

The code now after fetching data from excel column the month is in text as "Dec" that is "17-Dec-2018" I expect the final output in string as "2018-12-17" so that I can store in MYSQL database as DATE Type.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Emmanuel
  • 11
  • 3
  • 1
    For storing into MySQL use a `LocalDate` and `yourPreparedStatement.setObject(5, yourLocalDate);`. So what you need is to parse your string into a `LocalDate`. That has been asked and answered many times already, so just search and find. – Ole V.V. Mar 23 '19 at 08:34
  • Partially a duplicate of [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2) – Ole V.V. Mar 23 '19 at 08:48
  • 2
    SQL supports date/time objects, you don't need to convert it. Make use of the JDBC functionality and let the driver deal with it – MadProgrammer Mar 23 '19 at 09:21

3 Answers3

2

java.time

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("d-MMM-uuuu", Locale.ENGLISH);

    String fullName = "Emmanuel Macron";
    int idNumber = 42;
    String entryDateString = "17-Dec-2018";

    LocalDate entryDate = LocalDate.parse(entryDateString, dateFormatter);

    PreparedStatement insertStatement = yourDbConnection.prepareStatement(
            "insert into your_table (name, id, entry) values (?, ?, ?);");
    insertStatement.setString(1, fullName);
    insertStatement.setInt(2, idNumber);
    insertStatement.setObject(3, entryDate);
    int rowsInserted = insertStatement.executeUpdate();

The example is a bit simpler than yours, but should answer what you are asking about, so I trust the rest to you. I am using (and warmly recommending) java.time, the modern Java date and time API, for all date work in Java.

The steps involved for the date are:

  1. Parse the date string into a LocalDate. LocalDate.parse(entryDateString, dateFormatter) does this. In the format pattern string, d-MMM-uuuu d means day of month in 1 or 2 digits, MMM means month abbreviation, and uuuu means 4 digit year. The month abbreviation is locale specific. Since I took Dec to be English, I have specified English locale for the formatter; please correct if your date strings are in some other language.
  2. Pass the LocalDate to your PreparedStatement. insertStatement.setObject(3, entryDate); does this.

If you want to check that the first point worked as expected:

    System.out.println("Entry date was parsed into " + entryDate);

Output:

Entry date was parsed into 2018-12-17

PS You may also want to check whether you can get the date from Excel in a different way. If you are using an older library such as Apache POI, I am afraid that the other option is an old-fashioned Date object, which you would then need to convert, so it’s a question whether it’s worth it.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

Your code is just simple. Here is code:

Parse string input.

String fromDate="17-Dec-2018";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH);
LocalDate localDate = LocalDate.parse(fromDate, dateFormatter);

Generate string output.

String convertedDate = localDate.toString();
System.out.println(convertedDate);

Here DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH) is the input date format and locale.

Here is the imports:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
0

EDIT: Please follow the answer of @Ole V.V. given above as it is a better solution than mine.


You will first have to convert the String data into Date object. Example code:

    String sDate1="31/12/1998";  
    Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);  

Then you can use SimpleDateFormat class to format date as you wish.

For more details on SimpleDateFormatter class, check this out

Saeed Jassani
  • 1,079
  • 2
  • 11
  • 27
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Mar 23 '19 at 08:33
  • Kindly help me how to "convert the String data into Date object" – Emmanuel Mar 23 '19 at 08:34
  • 1
    @Emmanuel That’s not what you want to do. The `Date` class too is poorly designed and long outdated. – Ole V.V. Mar 23 '19 at 08:57
  • @OleV.V. SimpleDateFormat is not deprecated. It is the easiest way I found and I am using it in my Android application published on Play Store. – Saeed Jassani Mar 23 '19 at 08:59
  • 1
    @SaeedJassani The adoption of JSR 310 made the old date-time classes such as `SimpleDateFormat` and `Date` legacy. They were supplanted entirely by the *java.time* classes. Sun, Oracle, and the JCP community decided to move on; so should you. Later Android includes *java.time* classes. For Java 6 & 7, see the *ThreeTen-Backport* project. For early Android, see the *ThreeTenABP* project. – Basil Bourque Mar 23 '19 at 15:07