Netbeans数据库日期时间问题

时间:2013-04-19 14:44:03

标签: java database datetime arraylist format

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

从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!");

1 个答案:

答案 0 :(得分: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();
    }

Javadoc SimpleDateFormat