简体   繁体   English

日期转换格式Java转DB不同格式

[英]Date conversion format Java to DB different format

I'm making a code in java that executes an Oracle database procedure The format I have to put there in the procedure when I run through the database is dd/MM/yyyy.我正在 java 中编写代码,该代码执行 Oracle 数据库程序当我运行数据库时,我必须在程序中放入的格式是 dd/MM/yyyy。 I have to send this date from my java code using a CallebleStatement setDate with the date yyyy-MM-dd (which is the Date format in java) When this CallebleStatement is executed, will it transform the date I'm sending to the database format, or will it write to the Java format?我必须从我的 java 代码中使用带有日期 yyyy-MM-dd 的 CallebleStatement setDate 发送此日期(这是 java 中的日期格式)当执行此 CallebleStatement 时,它会将我发送的日期转换为数据库格式,还是会写入 Java 格式?

EDIT: It is basically this:编辑:基本上是这样的:

if(types[1].compareTo("DATE") == 0) {//procedureValues[i] = yyyy-MM-dd HH:mm:ss
    cs.setDate(i+1, Date.valueOf(procedureValues[i].trim()));
}else {
    cs.setObject(i+1, procedureValues[i].trim(), SQLTypes.valueOf(types[1]).getTypes());    
}

And yes, the procedure expects the DATE format是的,程序需要 DATE 格式

In Oracle, a DATE is a binary data type composed of 7 bytes (century, year-of-century, month, day, hour, minute and second).在 Oracle 中, DATE是由 7 个字节(世纪、世纪、月、日、小时、分钟和秒)组成的二进制数据类型。 It ALWAYS has those components and it NEVER stores a format.总是有这些组件,它从不存储格式。

When this CallebleStatement is executed, will it transform the date I'm sending to the database format, or will it write to the Java format?执行此 CallebleStatement 时,它会将我发送的日期转换为数据库格式,还是写入 Java 格式?

If you are using CallableStatement.setDate() , it will transform it to the binary values required by the database.如果您使用CallableStatement.setDate() ,它会将其转换为数据库所需的二进制值。 It will NOT store it in an (easily) human readable format.不会以(容易)人类可读的格式存储它。

If you want to see how the database stores the value then you can use:如果您想查看数据库如何存储值,则可以使用:

SELECT DUMP(your_date_column) FROM your_table;

When you read the value from the database then the client application (in this case, Java via the database driver) will read the binary values from the database and will convert it to a representation suitable to the client application.当您从数据库中读取值时,客户端应用程序(在本例中为 Java 通过数据库驱动程序)将从数据库中读取二进制值并将其转换为适合客户端应用程序的表示形式。

If you read it in a different client application then the binary values will be converted to the appropriate representation for that client application;如果您在不同的客户端应用程序中读取它,那么二进制值将被转换为该客户端应用程序的适当表示; so two different client applications (for example, Java and SQL/Plus) may display the same binary date value with different formats.因此两个不同的客户端应用程序(例如,Java 和 SQL/Plus)可能会以不同的格式显示相同的二进制日期值。

If the Date format in Java you're using is not compatible with the Date format of your database, the procedure will fail, because the procedure takes a String of dd/mm/yyyy and not a date instance.如果您使用的 Java 中的日期格式与数据库的日期格式不兼容,则该过程将失败,因为该过程采用dd/mm/yyyy字符串而不是日期实例。

You'll have to make sure your Java Date type is the correct from or transform it to match Oracle Date type dd/mm/yyyy and then use it as an argument in the procedure.您必须确保您的 Java 日期类型是正确的,或者将其转换为与 Oracle 日期类型dd/mm/yyyy匹配,然后将其用作过程中的参数。

An easy and simple solution is to use a String instead of a Date type in Java and use that String as an argument to the procedure, but that depends on your input and program一个简单的解决方案是在 Java 中使用字符串而不是日期类型,并将该字符串用作过程的参数,但这取决于您的输入和程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM