如何在Java中将日期转换为字符串从2018年12月17日到2018-12-17.我想将其存储到MYSQL数据库 [英] How to convert date as string from 17-Dec-2018 to 2018-12-17 in java. I want to store it to MYSQL database
问题描述
我已成功将日期列从excel导入到Java代码中.但是,我无法将日期格式从2018年12月17日更改为2018-12-17.请帮助.
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();
}
当月从excel列中获取数据后,代码现在以文本形式显示为"Dec",即"2018年12月17日" 我希望最终的字符串输出为"2018-12-17",以便我可以将DATE Type存储在MYSQL数据库中.
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.
推荐答案
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日期和时间API java.time,用于Java中的所有日期工作.
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.
该日期涉及的步骤是:
- 将日期字符串解析为
LocalDate
.LocalDate.parse(entryDateString, dateFormatter)
执行此操作.在格式模式字符串中,d-MMM-uuuu
d
表示月份中的1或2位数字,MMM
表示月份的缩写,uuuu
表示4位数字的年份.月的缩写是特定于区域设置的.由于我将Dec
设为英语,因此我为格式化程序指定了英语语言环境.如果您的日期字符串使用其他语言,请更正. - 将
LocalDate
传递到您的PreparedStatement
.insertStatement.setObject(3, entryDate);
这样做.
- 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, anduuuu
means 4 digit year. The month abbreviation is locale specific. Since I tookDec
to be English, I have specified English locale for the formatter; please correct if your date strings are in some other language. - Pass the
LocalDate
to yourPreparedStatement
.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);
输出:
输入日期被解析为2018-12-17
Entry date was parsed into 2018-12-17
PS您可能还想检查是否可以通过其他方式从Excel获取日期.如果您使用的是Apache POI之类的较旧的库,恐怕另一个选择是老式的Date
对象,然后需要转换该对象,因此是否值得这样做是一个问题.
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.
链接: Oracle教程:日期时间解释如何使用java.time.
Link: Oracle tutorial: Date Time explaining how to use java.time.
这篇关于如何在Java中将日期转换为字符串从2018年12月17日到2018-12-17.我想将其存储到MYSQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!