gpt4 book ai didi

java - 在 Java 中创建 .CSV 文件时日期格式受到干扰

转载 作者:行者123 更新时间:2023-11-30 02:58:06 26 4
gpt4 key购买 nike

我正在创建一个网络抓取工具,然后将数据存储在 .CSV 文件中。我的程序运行良好,但是存在一个问题,即我检索数据的网站的日期采用 (月日,年) 格式。因此,当我将数据保存在 .CSV 文件中时,它会将年份视为另一列,因此所有数据都会被操纵。我实际上想将该数据存储到 (MM-MON-YYYY) 中,并将有效日期存储在一列中。我在下面发布我的代码。请帮帮我。谢谢!

P.S:很抱歉没有在原帖中写出我想要的格式。

package com.mufapscraping;

//import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
//import java.util.Collections;
import java.util.Iterator;
//import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ComMufapScraping {

boolean writeCSVToConsole = true;
boolean writeCSVToFile = true;
//String destinationCSVFile = "C:\\convertedCSV.csv";
boolean sortTheList = true;
boolean writeToConsole;
boolean writeToFile;
public static Document doc = null;
public static Elements tbodyElements = null;
public static Elements elements = null;
public static Elements tdElements = null;
public static Elements trElement2 = null;
public static String Dcomma = ", 2";
public static ArrayList<Elements> sampleList = new ArrayList<Elements>();

public static void createConnection() throws IOException {
System.setProperty("http.proxyHost", "191.1.1.123");
System.setProperty("http.proxyPort", "8080");
String tempUrl = "http://www.mufap.com.pk/nav_returns_performance.php?tab=01";
doc = Jsoup.connect(tempUrl).get();
}

public static void parsingHTML() throws Exception {
for (int i = 1; i <= 1; i++) {

tbodyElements = doc.getElementsByTag("tbody");
//Element table = doc.getElementById("dataTable");

if (tbodyElements.isEmpty()) {
throw new Exception("Table is not found");
}
elements = tbodyElements.get(0).getElementsByTag("tr");

for (Element trElement : elements) {
trElement2 = trElement.getElementsByTag("tr");
tdElements = trElement.getElementsByTag("td");
FileWriter sb = new FileWriter("C:\\convertedCSV2.csv", true);
for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) {
if (it.hasNext()) {
sb.append(" \n ");
}
for (Iterator<Element> it2 = trElement2.iterator(); it.hasNext();) {
Element tdElement = it.next();
sb.append(tdElement.text());
if (it2.hasNext()) {
sb.append(" , ");
}

}

System.out.println(sb.toString());
sb.flush();
sb.close();
}

System.out.println(sampleList.add(tdElements));
/* for (Elements elements2 : zakazky) {
System.out.println(elements2);
}*/

}
}
}

public static void main(String[] args) throws IOException, Exception {
createConnection();
parsingHTML();

}

}

enter image description here

最佳答案

不要直接在 FileWriter 中附加元素文本,而是先对其进行格式化,然后附加它。

因此,替换以下行:

sb.append(tdElement.text());

进入

sb.append(formatData(tdElement.text()));
<小时/>
private static final SimpleDateFormat FORMATTER_MMM_d_yyyy = new SimpleDateFormat("MMM d, yyyy", Locale.US);
private static final SimpleDateFormat FORMATTER_dd_MMM_yyyy = new SimpleDateFormat("dd-MMM-YYYY", Locale.US);

public static String formatData(String text) {
String tmp = null;

try {
Date d = FORMATTER_MMM_d_yyyy.parse(text);
tmp = FORMATTER_dd_MMM_yyyy.format(d);
} catch (ParseException pe) {
tmp = text;
}

return tmp;
}

示例

public static void main(String[] args) {
String[] fields = new String[] { //
"ABL Cash Fund", //
"AA(f)", //
"Apr 18, 2016", //
"10.4729" //
};

for (String field : fields) {
System.out.format("%s\n%s\n\n", field, formatData(field));
}
}

输出

ABL Cash Fund
ABL Cash Fund

AA(f)
AA(f)

Apr 18, 2016
18-Apr-2016

10.4729
10.4729

关于java - 在 Java 中创建 .CSV 文件时日期格式受到干扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36691942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com