简体   繁体   English

如何以yyyy-MM-dd HH:mm:ss格式从Oracle数据库检索Date变量并以相同格式存储在Date变量中?

[英]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.

相关问题 如何将日期和时间格式从“ yyyy-MM-dd HH:mm:ss”更改为“ h:mm a”? - How to change date and time format from“yyyy-MM-dd HH:mm:ss” to “h:mm a”? 从任何格式以 yyyy-MM-dd hh:mm:ss 格式格式化日期 - Format date in yyyy-MM-dd hh:mm:ss format from whatever format Oracle日期格式更改为yyyy-MM-dd hh:mm:ss - Oracle Date Format change to yyyy-MM-dd hh:mm:ss 以oracle和PostgreSQL查询以yyyy-MM-dd'T'HH:mm:ss.SSSZ格式打印日期 - Query to print date in yyyy-MM-dd'T'HH:mm:ss.SSSZ format in oracle and postgresql 如何将 java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) 格式化为日期(yyyy-MM-dd HH:mm:ss) - How to format a java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) to a date(yyyy-MM-dd HH:mm:ss) 如何将格式日期“MMddhhmmss”转换为“yyyy-MM-dd HH:mm:ss”? - How to convert format date “MMddhhmmss” to “yyyy-MM-dd HH:mm:ss”? 日期从yyyy-MM-dd HH:mm:ss转换为ISO日期yyyy-MM-dd'T'HH:mm:ssXXX格式问题 - Date conversion from yyyy-MM-dd HH:mm:ss to ISO date yyyy-MM-dd'T'HH:mm:ssXXX format issue 如何解析 yyyy-MM-dd HH:mm:ss 到日期? - How to parse a yyyy-MM-dd HH:mm:ss to Date? 在Java 7中将字符串日期转换为yyyy-MM-dd'T'HH:mm:ss.SSSSSS格式的字符串 - Converting string date to string in yyyy-MM-dd'T'HH:mm:ss.SSSSSS format in java 7 YYYY-MM-DD HH:MM:SS[.fraction] 格式的 MySQL 更新日期字段 - MySQL update date field in YYYY-MM-DD HH:MM:SS[.fraction] format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM