-1

I am creating a attendance management system for our company and the attendance of every employee are on a text file generated by a finger print biometric machine. The problem is that I am trying to convert arrays of string dates from the text file and save it to database but it gives off the "unparseable date error". Please see code for reference:

public void getReadTextFile(String path) throws ParseException{
    sql = new String("INSERT INTO logs(EmpID, ValidDate, ValidTime,Processed, PRDate) VALUES(?,?,?,?,?)");
    BufferedReader br = null;
    String[] ID = new String[3000];
    java.sql.Date[] vDate = new java.sql.Date[3000];
    Time[] vTime = new Time[3000];
    Date date = new Date();
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    try{
        FileInputStream fsStream = new FileInputStream(path);
        br = new BufferedReader(new InputStreamReader(fsStream));
        String strLine;
        br.readLine();
        int i = 0;
        while((strLine = br.readLine()) != null){
            String[] arr = strLine.split("\\s+");
            java.util.Date qDate = format.parse(arr[5]);
            ID[i] = arr[2];
            vDate[i] = new java.sql.Date(qDate.getTime());
            vTime[i] = java.sql.Time.valueOf(arr[6]);
            java.sql.Date sqldate = new java.sql.Date(date.getTime());

            pstmt = data.prepareStatement(sql);
            pstmt.setString(1, arr[2]);
            pstmt.setDate(2, vDate[i]);
            pstmt.setTime(3, vTime[i]);
            pstmt.setInt(4, 1);
            pstmt.setDate(5, sqldate);
        }
        i++;

    }catch(IOException ex){
        System.out.println(ex.getMessage());
    }catch(SQLException ev){
        //System.out.println(ev.getMessage());
        ev.printStackTrace();
    }catch(ParseException pe){
        pe.printStackTrace();
    }
    finally{
        try{
            if(br != null){
                br.close();
            }
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
    }
}

This is the stacktrace:

java.text.ParseException: Unparseable date: "2016/12/19"
at java.text.DateFormat.parse(Unknown Source)
at Model.ProcessDB.getReadTextFile(ProcessDB.java:94)
at View.Dashboard$2.actionPerformed(Dashboard.java:187)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
SilverRay
  • 331
  • 2
  • 7
  • 23

1 Answers1

2

If you are specifying the dates like "2017-04-25" then use "yyyy-MM-dd" instead of "yyyy-mm-dd"

 DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Where,

M- Month in year. Number of M's determine length of format (e.g. MM, MMM or MMMMM)
m- Minute in hour, 0-59 (normally mm)

If you are specifying the dates like "2017/04/25" then use "yyyy/MM/dd" instead of "yyyy-mm-dd"

 DateFormat format = new SimpleDateFormat("yyyy/MM/dd");
ProgrammerBoy
  • 876
  • 6
  • 19
  • My problem is trying to convert arrays of string dates to dates. – SilverRay Apr 26 '17 at 03:51
  • @SilverRay No, your problem is the format used to convert (parse) each date. As the exception said : `java.text.ParseException: Unparseable date: "2016/12/19"` since you use this pattern `"yyyy-mm-dd"` – AxelH Apr 26 '17 at 05:30
  • Sorry I forgot to correct that. I already replaced it with your suggestion but it still throw the same error. – SilverRay Apr 26 '17 at 05:31
  • If you look at the second sentence of the stack trace it said that "at java.text.DateFormat.parse(Unknown Source)". – SilverRay Apr 26 '17 at 05:33
  • @SilverRay Check the pattern closely, `Sanked D` made a mistake too. Your separator should be `/` not `-` so use `"yyyy/MM/dd"`. – AxelH Apr 26 '17 at 05:34
  • @AxeIH, i didn't had idea as there was no data in question. Based on simpledateformat in his question, i thought he want to parse date in "yyyy-MM-dd" format. I just corrected the mistake he made in his program. Update my answer to include SDF for "yyyy/MM/dd" too. SilverRay...use the one at bottom if you want to parse for example "2017/04/26". – ProgrammerBoy Apr 26 '17 at 06:15
  • Sorry, I thought like PHP, it will convert the "2016/12/19" to "2016-12-19". Thanks for the solution... – SilverRay Apr 26 '17 at 06:45
  • Am glad it helped! – ProgrammerBoy Apr 26 '17 at 06:48