当我尝试从MySQL检索时间并将其设置为JLabel
时,它给了我一个错误。
java.sql.SQLException:java.sql.Time类型的非法小时值'50'
值'50:51:05。
谁能建议我如何解决此问题?
代码如下。
String sql = "SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(ot)))"
+ " FROM attendance"
+ " WHERE department = '"+department+"'"
+ " AND date BETWEEN '"+dateChooser1+"' AND '"+dateChooser2+"'";
st = con.createStatement();
rst = st.executeQuery(sql);
if(rst.next())
{
String time = rst.getString(1);
oTimeTemp.setText(time);
}
最佳答案
我解决了这是代码,
String sql = "SELECT SUM(TIME_TO_SEC(ot))"
+ " FROM attendance"
+ " WHERE department = '"+department+"'"
+ " AND date BETWEEN '"+dateChooser1+"' AND '"+dateChooser2+"'";
st = con.createStatement();
rst = st.executeQuery(sql);
if(rst.next())
{
String time = rst.getString(1);
Double dTime = Double.valueOf(time);
int intTime = (int) dTime.doubleValue();
String nTime = calculateTime(intTime);
oTimeTemp.setText(nTime);
}
private static String calculateTime(int totalSecs)
{
int hours = totalSecs / 3600;
int minutes = (totalSecs % 3600) / 60;
int seconds = totalSecs % 60;
String timeString = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return timeString;
}