Java 如何从一列中分离两个日期-从外部文件读取

Java 如何从一列中分离两个日期-从外部文件读取,java,arrays,date,date-parsing,dateinterval,Java,Arrays,Date,Date Parsing,Dateinterval,到目前为止,我已经能够从一个有两列的外部excel文件中读取数据 身份证日期 1 2018年12月12日2018年12月13日 2 2018年12月12日2018年12月13日 3 2018年12月12日2018年12月13日 2018年12月4日2018年12月13日 5 2018年12月12日2018年12月13日 2018年12月6日2018年12月13日 7 2018年12月12日2018年12月13日 我的问题是,有没有办法将包含两个日期的一列拆分为另一列,或者用不同的方法将其分开?以

到目前为止,我已经能够从一个有两列的外部excel文件中读取数据

身份证日期
1 2018年12月12日2018年12月13日
2 2018年12月12日2018年12月13日
3 2018年12月12日2018年12月13日
2018年12月4日2018年12月13日
5 2018年12月12日2018年12月13日
2018年12月6日2018年12月13日
7 2018年12月12日2018年12月13日

我的问题是,有没有办法将包含两个日期的一列拆分为另一列,或者用不同的方法将其分开?以上是一个原型,答案方法将用于大数据。我们将非常感谢您的帮助

目前使用的代码:

package readexcel;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;


public class Readexcel{

@SuppressWarnings({ "unchecked", "unchecked" })
public static void main(String[] args) throws Exception {

//
// An excel file name. You can create a file name with a full
// path information.
//
String filename = "C:\\filepath...xls";

//
// Create an ArrayList to store the data read from excel sheet.
//
List sheetData = new ArrayList();
FileInputStream fis = null;
try {
//
// Create a FileInputStream that will be use to read the
// excel file.
//
fis = new FileInputStream(filename);

//
// Create an excel workbook from the file system.
//
HSSFWorkbook workbook = new HSSFWorkbook(fis);
//
// Get the first sheet on the workbook.
//
HSSFSheet sheet = workbook.getSheetAt(0);

//
// When we have a sheet object in hand we can iterator on
// each sheet's rows and on each row's cells. We store the
// data read on an ArrayList so that we can printed the
// content of the excel to the console.
//
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();

List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}

sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}

showExcelData(sheetData);
}

private static void showExcelData(List sheetData) {
//
// Iterates the data and print it out to the console.
//
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
Cell cell = (Cell) list.get(j);
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
System.out.print(cell.getNumericCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.print(cell.getRichStringCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
System.out.print(cell.getBooleanCellValue());
}
if (j < list.size() - 1) {
System.out.print(", ");
}
}
System.out.println("");
}
}
}
package-readexcel;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Cell;
公共类Readexcel{
@SuppressWarnings({“未选中”、“未选中”})
公共静态void main(字符串[]args)引发异常{
//
//excel文件名。您可以创建具有完整名称的文件名
//路径信息。
//
String filename=“C:\\filepath…xls”;
//
//创建ArrayList以存储从excel工作表读取的数据。
//
List sheetData=new ArrayList();
FileInputStream fis=null;
试一试{
//
//创建一个FileInputStream,用于读取
//excel文件。
//
fis=新文件输入流(文件名);
//
//从文件系统创建excel工作簿。
//
HSSF工作手册=新的HSSF工作手册(fis);
//
//获取工作簿上的第一张工作表。
//
HSSFSheet sheet=工作簿。getSheetAt(0);
//
//当我们手头有一个sheet对象时,我们可以对其进行迭代
//每个工作表的行和每行的单元格上。我们存储
//在ArrayList上读取数据,以便打印
//将excel的内容发送到控制台。
//
迭代器行=sheet.rowIterator();
while(rows.hasNext()){
HSSFRow行=(HSSFRow)行。下一步();
迭代器单元格=行。单元格迭代器();
列表数据=新的ArrayList();
while(cells.hasNext()){
HSSFCell单元格=(HSSFCell)单元格。下一步();
添加数据(单元格);
}
sheetData.add(数据);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
如果(fis!=null){
fis.close();
}
}
showceldata(sheetData);
}
专用静态void showExcelData(列表数据){
//
//迭代数据并将其打印到控制台。
//
对于(int i=0;i
这将产生:

First date: 2018-12-12 Second date: 2018-12-13
First date: 2018-12-12 Second date: 2018-12-13
First date: 2018-12-12 Second date: 2018-12-13

我首先使用正则表达式从字符串中选择两个日期。接下来我想使用
LocalDate.parse
来解析和验证日期。您可能需要添加范围检查以进行进一步验证。如果是这样,请使用
LocalDate.isBefore
和/或
.isAfter

这两个日期是用一个空格分隔的吗?是的,大多数时间都是其他日期示例:2018年12月12日-2018年12月13日,有时是2018年12月12日-2018年12月13日。一个建议是:使用类似
(\d+/\d+/\d+/\d+)\d+\d+/\d+/\d+)
获取两个日期,然后将每个日期解析为一个
LocalDate
进行验证。您是要创建另一个Excel文件,还是只想拆分字符串?有一个
split()
方法。
First date: 2018-12-12 Second date: 2018-12-13
First date: 2018-12-12 Second date: 2018-12-13
First date: 2018-12-12 Second date: 2018-12-13