0

编译时收到的错误消息是“日期时间值的字符串表示形式的语法不正确”

从 History ArrayList 传入日期对象时,其格式为 '19-Apr-2013'

我知道数据库的格式是“年-月-日”。有什么方法可以将我的日期对象转换为数据库的正确格式?

for ( Patient p: pList )
{
    patientInsertSQL = "Insert Into SHAUN.PATIENT VALUES (" + p.getPatientNum() +
        + ", '" + p.getPatientName() + "', '" + p.getPatientAddress() + "', '"
        + p.getPatientPhone() + "')";
    res = stmt.executeUpdate(patientInsertSQL);
    System.out.println(res);
    ArrayList<History> tempHistList = p.getHistory();
    String histSelect = "select * from SHAUN.HISTORY";
    result = stmt.executeQuery(histSelect);
    for ( History h: tempHistList )
    {
        historyInsertSQL = "Insert Into SHAUN.HISTORY VALUES (" + h.getHistID()
          + ", '" + h.getConditionName() + "', '" + h.getMedication() + "', '"
          + h.getDateOccured() + "', " + p.getPatientNum() + ")";
        res = stmt.executeUpdate(historyInsertSQL);
        System.out.println(res);
        //Loop Checker
        int i = 1;
        System.out.println("In the History Loop " + i);
        i++;
    }
    System.out.println("In the loop!");
4

1 回答 1

0

阅读这篇文章SimpleDateFormat

它应该可以帮助您获得想要实现的目标。

例如:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
try {
     date = formatter.parse(yourdate.toString());
     formatter.applyPattern("yyyy-MM-dd");
     dateformat = formatter.format(yourdate);
     System.err.println(dateformat);

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

SimpleDateFormat的 Javadoc

于 2013-04-19T14:59:23.217 回答