How to convert date as string from 17-Dec-2018 to 2018-12-17 in java. I want to store it to MYSQL database

谁都会走 提交于 2019-12-24 20:32:39

问题


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.


回答1:


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.




回答2:


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;



回答3:


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



来源:https://stackoverflow.com/questions/55311948/how-to-convert-date-as-string-from-17-dec-2018-to-2018-12-17-in-java-i-want-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!