Menu

#102 milliseconds values lost when parsing times with "HH:mm:ss.SSS"

None
closed
None
5
2014-05-23
2014-04-22
No

My test file sample.csv contains some time values:

T
14:21:07.034
14:21:07.858
14:21:07.973

If I run the following test program, the milliseconds values of the times are lost and are all zero, even though I specify milliseconds in all format strings.

package org.relique.jdbc.csv;

import java.sql.Connection;

public class Runner2
{
    public static void main(String []args)
    {
            try
            {
             // Load the driver.
            Class.forName("org.relique.jdbc.csv.CsvDriver");

            // Create a connection. The first command line parameter is
            // the directory containing the .csv files.
            // A single connection is thread-safe for use by several threads.
            Properties props = new Properties();
            props.put("columnTypes", "Time");
            props.put("timeFormat", "HH:mm:ss.SSS");
            Connection conn = DriverManager.getConnection("jdbc:relique:csv:/tmp", props);

            // Create a Statement object to execute the query with.
            // A Statement is not thread-safe.
            Statement stmt = conn.createStatement();

            // Select the ID and NAME columns from sample.csv
            ResultSet results = stmt.executeQuery("SELECT T FROM sample");

           // Dump out the results to a CSV file with the same format
            // using CsvJdbc helper function
            SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss.SSS");
            while (results.next())
            {
                    Time t = (Time)results.getTime(1);
                    System.out.println(f.format(t));
            }

            // Clean up
            results.close();
            stmt.close();
            conn.close();
            }
            catch (Exception e)
            {
                    e.printStackTrace();
            }
    }
}

The output of the program is:

14:21:07.000
14:21:07.000
14:21:07.000

Discussion

  • Simon Chenery

    Simon Chenery - 2014-04-24

    Fix parsing of times containing milliseconds.

    Added unit test TestStringConverter.testParseTimeMilliseconds.

    Files changed:
    src/main/java/org/relique/jdbc/csv/StringConverter.java
    src/test/java/org/relique/jdbc/csv/TestStringConverter.java

     
  • Simon Chenery

    Simon Chenery - 2014-04-24
    • status: open --> pending
    • assigned_to: Simon Chenery
    • Group: -->
     
  • Simon Chenery

    Simon Chenery - 2014-05-23

    Included in CsvJdbc version 1.0-20

     
  • Simon Chenery

    Simon Chenery - 2014-05-23
    • status: pending --> closed