[英]How to retrieve the Date variable from oracle database in yyyy-MM-dd HH:mm:ss format and store in Date variable in the same format?
I want to retrieve the Date object from oracle database and store it in Date variable in Java in the same format ? 我想从oracle数据库检索Date对象并将其以相同格式存储在Java中的Date变量中吗?
PS ; PS; Data type of the variable should be Date only but, need to have the format yyyy-MM-dd HH:mm:ss .
变量的数据类型应仅为Date,但必须采用yyyy-MM-dd HH:mm:ss格式。
Date
class will store internally the date as a long integer. Date
类将内部日期存储为长整数。 If you want to convert it to string you can use a DateFormat
如果要将其转换为字符串,可以使用
DateFormat
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateFormatted = df.format(new Date());
Date date = df.parse(dateFormatted);
If the database is set up correctly, you should be able to simply retrieve a date column into a date variable. 如果数据库设置正确,则应该可以简单地将date列检索到date变量中。 Formatting considerations should be non-applicable at the database access level;
格式化注意事项应在数据库访问级别不适用; they should be a non-issue.
它们应该不是问题。
Formatting is something you do in order to display the date to the user. 格式化是您执行的操作,以便向用户显示日期。
Formatting is also something that various tools do, to show you the contents of your database, or the contents of your date object, so this can cause a bit of confusion, as it may lead you to believe that the date column has a certain format, or that your date variable has a certain format. 格式化也是各种工具用来向您显示数据库的内容或日期对象的内容的方式,因此这可能会引起一些混乱,因为它可能使您相信date列具有某种格式,或者您的日期变量具有某种格式。 But they don't.
但是他们没有。
The problems start when things are done wrongly at the database level. 当在数据库级别执行错误操作时,问题就开始了。 If the database column is not actually of a temporal data type, but of
CHAR
type instead, then of course formatting plays a role. 如果数据库列实际上不是临时数据类型,而是
CHAR
类型,那么格式化当然会起作用。 But then we are not talking about "Retrieving a date object from the oracle database", we are talking about retrieving a string from the database and converting it to a date. 但是然后我们不是在谈论“从oracle数据库中检索日期对象”,而是在谈论从数据库中检索字符串并将其转换为日期。 However, you have no told us nothing about the structure of your table, so we cannot speculate about that.
但是,您没有告诉我们有关表结构的任何信息,因此我们无法对此进行推测。
So, with the information that you have given, the right answer is simply "just fetch the date column into a date variable". 因此,有了您提供的信息,正确的答案就是“仅将date列提取到date变量中”。 Please provide more information if you need a different answer.
如果您需要其他答案,请提供更多信息。
As per the comments, you might want to create a CustomDate
class that prints the value in the specified format: 根据注释,您可能需要创建一个
CustomDate
类,以指定格式打印值:
public class CustomDate extends Date{
private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public String toString() {
return df.format(this);
}
}
Instead of creating instances of Date, try to use CustomDate. 与其创建Date实例,不如尝试使用CustomDate。 It'll be equivalent in your code.
它在您的代码中是等效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.