我正在为公司创建一个考勤管理系统,每个员工的出勤都在指纹生物识别机生成的文本文件中。问题是我正在尝试从文本文件转换字符串日期数组并将其保存到数据库,但它会发出"无法解析的日期错误"。请参阅代码以供参考:
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());
}
}
}
这是堆栈跟踪:
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)
答案 0 :(得分:2)
如果您指定“2017-04-25”之类的日期,请使用"yyyy-MM-dd"
代替"yyyy-mm-dd"
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
其中,
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)
如果您指定的日期如“2017/04/25”,请使用"yyyy/MM/dd"
代替"yyyy-mm-dd"
DateFormat format = new SimpleDateFormat("yyyy/MM/dd");