簡體   English   中英

從InputStream解析日期時發生ParseException

[英]ParseException When Parsing a Date from InputStream

在JUnit測試期間,我遇到了與此帖子類似的問題,其中日期似乎可以由SimpleDateFormat解析,但是我收到了ParseException的說法:

java.text.ParseException: Unparseable date: "05-13-2013"

我在Java 6下運行。

受測試的類FileMoveBasedOnControlFile具有函數getDateStringEntriesFromStream ,該函數getDateStringEntriesFromStream InputStream,嘗試使用MM-dd-yyyy格式將該Stream中的每一行解析為Date,將成功解析的每個日期轉換為新格式yyyy-MM-dd 。最后將成功轉換的日期輸出到ArrayList。

'2013年5月11日'似乎可以正確解析。 測試“ 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"));
    }

這是正在測試的類函數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM