如何在java中将日期转换为字符串,从2018年12月17日到2018-12-17。我想将它存储到MYSQL数据库

问题描述 投票:0回答:3

我已成功将日期列从excel导入到java代码中。但是,我无法将日期格式从2018年12月17日更改为2018-12-17。请帮助。

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

现在从excel列获取数据后的代码当月是文本“Dec”即“17-Dec-2018”我希望字符串中的最终输出为“2018-12-17”,以便我可以存储在MYSQL数据库中作为日期类型。

java date date-conversion java-date
3个回答
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();

这个例子比你的简单一点,但应该回答你的问题,所以我相信其余的事情。我正在使用(并热烈推荐)java.time,即现代Java日期和时间API,用于Java中的所有日期工作。

该日期涉及的步骤是:

  1. 将日期字符串解析为LocalDateLocalDate.parse(entryDateString, dateFormatter)这样做。在格式模式字符串中,d-MMM-uuuu d表示1或2位数的日期,MMM表示月份缩写,uuuu表示4位数年份。月份缩写是特定于语言环境的。由于我把Dec作为英语,我已经为格式化程序指定了英语语言环境;如果你的日期字符串是用其他语言,请更正。
  2. LocalDate传递给你的PreparedStatementinsertStatement.setObject(3, entryDate);这样做。

如果要检查第一个点是否按预期工作:

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

输出:

报名日期分析为2018-12-17

PS您可能还想检查是否可以以不同的方式从Excel获取日期。如果您使用的是较旧的库,例如Apache POI,我恐怕另一个选项是一个老式的Date对象,您需要转换它,所以这是一个值得它的问题。

链接:Oracle tutorial: Date Time解释如何使用java.time。


1
投票

你的代码很简单。这是代码:

解析字符串输入。

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

生成字符串输出。

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

这里DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH)是输入日期格式和区域设置。

这是进口:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

0
投票

编辑:请按照@Ole V.V的回答。如上所述,因为它比我的解决方案更好。


您首先必须将String数据转换为Date对象。示例代码:

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

然后你可以使用SimpleDateFormat类来格式化日期。

有关SimpleDateFormatter类的更多详细信息,请查看this

© www.soinside.com 2019 - 2024. All rights reserved.