从 InputStream 解析日期时出现 ParseException
ParseException When Parsing a Date from InputStream
在 JUnit 测试期间,我 运行 遇到了一个类似于此 post 的问题,其中日期似乎可以由 SimpleDateFormat 解析,但我收到一个 ParseException 说:
java.text.ParseException: Unparseable date: "05-13-2013"
我 运行 低于 Java 6.
测试中的 class FileMoveBasedOnControlFile
有一个函数 getDateStringEntriesFromStream
它接受一个 InputStream,尝试使用 [=15= 格式将该流中的每一行解析为日期],将每个解析成功的日期转换成新的格式yyyy-MM-dd
,最后将转换成功的日期输出到一个ArrayList。
'05-11-2013' 似乎解析得很好。测试“05-13-2013”中的下一个日期没有。我不知所措,InputStream(或“\n”)似乎不会影响这段代码。我试过“\r\n”,但也没有用。
在测试期间,以下代码将解析第一个而不是第二个日期:
@Test
public void testMultipleValidEntries() throws IOException
{
StringBuilder strBuilder = new StringBuilder();
String date1 = "05-11-2013";
String date2 = "05-13-2013";
String date3 = "05-16-2013";
strBuilder.append(date1 + "\n");
strBuilder.append(date2 + "\n");
strBuilder.append(date3);
FileMoveBasedOnControlFile fileMoveBasedOnControlFile = new FileMoveBasedOnControlFile();
InputStream inputStream = new ByteArrayInputStream(strBuilder.toString().getBytes("UTF-8"));
ArrayList<String> entries = fileMoveBasedOnControlFile.getDateStringEntriesFromStream(inputStream);
assertTrue(entries.size() == 3);
assertTrue(entries.get(0).equals("2013-05-11"));
assertTrue(entries.get(1).equals("2013-05-13"));
assertTrue(entries.get(2).equals("2013-05-16"));
}
这是正在测试的 class 函数:
public ArrayList<String> getDateStringEntriesFromStream(InputStream inputStream) throws IOException
{
ArrayList<String> controlFileEntries = new ArrayList<String>();
BufferedReader controlFileReader = new BufferedReader(new InputStreamReader(inputStream));
String controlFileEntry;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ORIGINAL_DATE_FORMAT);
simpleDateFormat.setLenient(false);
LOG.info("Reading stream.");
while( (controlFileEntry = controlFileReader.readLine()) != null)
{
try
{
Date controlFileDate = simpleDateFormat.parse(controlFileEntry);
simpleDateFormat.applyPattern(NEW_DATE_FORMAT);
String newDateString = simpleDateFormat.format(controlFileDate);
controlFileEntries.add(newDateString);
LOG.info("Got " + newDateString + ".");
}
catch(ParseException e)
{
LOG.info("Invalid date entry \'" + controlFileEntry + "\'.");
}
}
if (controlFileEntries.size() == 0)
{
LOG.info("Stream is empty.");
}
return controlFileEntries;
}
其中 ORIGINAL_DATE_FORMAT
是 'MM-dd-yyyy',NEW_DATE_FORMAT
是 'yyyy-MM-dd'。
将 SimpleDateFormat
声明移到循环中。它适用于第一个 Date
,但随后失败,因为它永远不会重新初始化为您的 ORIGINAL_DATE_FORMAT
LOG.info("Reading stream.");
while( (controlFileEntry = controlFileReader.readLine()) != null)
{
// Every iteration should start with the ORIGINAL_DATE_FORMAT
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ORIGINAL_DATE_FORMAT);
simpleDateFormat.setLenient(false);
在 JUnit 测试期间,我 运行 遇到了一个类似于此 post 的问题,其中日期似乎可以由 SimpleDateFormat 解析,但我收到一个 ParseException 说:
java.text.ParseException: Unparseable date: "05-13-2013"
我 运行 低于 Java 6.
测试中的 class FileMoveBasedOnControlFile
有一个函数 getDateStringEntriesFromStream
它接受一个 InputStream,尝试使用 [=15= 格式将该流中的每一行解析为日期],将每个解析成功的日期转换成新的格式yyyy-MM-dd
,最后将转换成功的日期输出到一个ArrayList。
'05-11-2013' 似乎解析得很好。测试“05-13-2013”中的下一个日期没有。我不知所措,InputStream(或“\n”)似乎不会影响这段代码。我试过“\r\n”,但也没有用。
在测试期间,以下代码将解析第一个而不是第二个日期:
@Test
public void testMultipleValidEntries() throws IOException
{
StringBuilder strBuilder = new StringBuilder();
String date1 = "05-11-2013";
String date2 = "05-13-2013";
String date3 = "05-16-2013";
strBuilder.append(date1 + "\n");
strBuilder.append(date2 + "\n");
strBuilder.append(date3);
FileMoveBasedOnControlFile fileMoveBasedOnControlFile = new FileMoveBasedOnControlFile();
InputStream inputStream = new ByteArrayInputStream(strBuilder.toString().getBytes("UTF-8"));
ArrayList<String> entries = fileMoveBasedOnControlFile.getDateStringEntriesFromStream(inputStream);
assertTrue(entries.size() == 3);
assertTrue(entries.get(0).equals("2013-05-11"));
assertTrue(entries.get(1).equals("2013-05-13"));
assertTrue(entries.get(2).equals("2013-05-16"));
}
这是正在测试的 class 函数:
public ArrayList<String> getDateStringEntriesFromStream(InputStream inputStream) throws IOException
{
ArrayList<String> controlFileEntries = new ArrayList<String>();
BufferedReader controlFileReader = new BufferedReader(new InputStreamReader(inputStream));
String controlFileEntry;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ORIGINAL_DATE_FORMAT);
simpleDateFormat.setLenient(false);
LOG.info("Reading stream.");
while( (controlFileEntry = controlFileReader.readLine()) != null)
{
try
{
Date controlFileDate = simpleDateFormat.parse(controlFileEntry);
simpleDateFormat.applyPattern(NEW_DATE_FORMAT);
String newDateString = simpleDateFormat.format(controlFileDate);
controlFileEntries.add(newDateString);
LOG.info("Got " + newDateString + ".");
}
catch(ParseException e)
{
LOG.info("Invalid date entry \'" + controlFileEntry + "\'.");
}
}
if (controlFileEntries.size() == 0)
{
LOG.info("Stream is empty.");
}
return controlFileEntries;
}
其中 ORIGINAL_DATE_FORMAT
是 'MM-dd-yyyy',NEW_DATE_FORMAT
是 'yyyy-MM-dd'。
将 SimpleDateFormat
声明移到循环中。它适用于第一个 Date
,但随后失败,因为它永远不会重新初始化为您的 ORIGINAL_DATE_FORMAT
LOG.info("Reading stream.");
while( (controlFileEntry = controlFileReader.readLine()) != null)
{
// Every iteration should start with the ORIGINAL_DATE_FORMAT
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ORIGINAL_DATE_FORMAT);
simpleDateFormat.setLenient(false);