When I store a Date field in Oracle database it always shows 00:00:00 hour ¿Is it problem of SimpleDateFormat class?

huangapple go评论73阅读模式
英文:

When I store a Date field in Oracle database it always shows 00:00:00 hour ¿Is it problem of SimpleDateFormat class?

问题

在我的Java操作文件中,我正在获取实际日期以将其存储在Oracle数据库中。然而,当我将日期存储在数据库中时,它总是显示为类似于30/05/2020,00:00:00的格式。我的意思是,它没有考虑当前的小时,始终显示为00:00:00。

这是获取日期的Java代码:

SimpleDateFormat sdffecha_modif = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String fechaModif = sdffecha_modif.format(new Date());

这是否是常见错误?有人知道发生了什么吗?

英文:

In my Java action file, I am obtaining the actual date to store it in an Oracle database. However, when I store the date in the database, it always appear like 30/05/2020, 00:00:00. I mean, it is not taking the current hour, always appearing 00:00:00.

This is the java code that obtains the date:

    SimpleDateFormat sdffecha_modif = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    String fechaModif = sdffecha_modif.format(new Date());

Is it a common error? Does anybody know what is happening?

答案1

得分: 1

您没有提供足够的详细信息和代码来诊断您的问题。我只能猜测,我可以修复两个主要问题。

java.sql.Datejava.util.Date

我的猜测:您可能正在使用 java.sql.Date 类,该类假装表示仅包含日期而没有时间和时区的值。

相比之下,java.util.Date 类表示UTC中的一个时刻,具有零分钟-小时-秒的时间和日期。

避免使用旧的日期时间类

永远不要使用 Date 类或 SimpleDateFormat。这些可怕的类在多年前已被现代的 java.time 类(JSR 310)取代。

使用智能对象,而不是愚蠢的字符串

与数据库交换智能对象,而不是愚蠢的字符串。从JDBC 4.2开始,我们可以交换 java.time 对象。

Oracle数据库中的 DATE 类型类似于SQL标准类型 TIMESTAMP WITHOUT TIME ZONE。请注意,这种类型不能表示一个时刻,不能用于时间线上的点。缺少时区或偏移意味着这些值只能表示全球各地大约26-27小时的时间范围。

java.time

Java中的等效类型是 LocalDateTime

LocalDate ld = LocalDate.of(2021, Month.MAY, 23);
LocalTime lt = LocalTime.of(12, 30);
LocalDateTime ldt = LocalDateTime.of(ld, lt);

写入数据库。

myPreparedStatement.setObject(  , ldt );

检索。

LocalDateTime ldt = myResultSet.getObject(  , LocalDateTime.class );

When I store a Date field in Oracle database it always shows 00:00:00 hour ¿Is it problem of SimpleDateFormat class?


关于 java.time

java.time 框架内置于Java 8及更高版本中。这些类取代了令人头痛的旧的legacy日期时间类,如 java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参阅Oracle教程。并在Stack Overflow上搜索许多示例和解释。规范是JSR 310

Joda-Time 项目现在处于维护模式,建议迁移到java.time类。

您可以直接与您的数据库交换 java.time 对象。使用与JDBC 4.2或更高版本兼容的JDBC驱动程序。不需要字符串,也不需要 java.sql.* 类。Hibernate 5 和 JPA 2.2 支持 java.time

获取java.time类的位置?

英文:

You have not posted enough details and code to diagnose your problem. I can only guess, and I can fix two major problems.

java.sql.Date versus java.util.Date

My guess: You may be using the java.sql.Date class that pretends to represent a date-only value without a time-of-day and without a time zone.

In contrast, the java.util.Date class represents a moment in UTC, a date with time-of-day on offset of zero minutes-hours-seconds.

Avoid legacy date-time classes

Never use either Date class, nor SimpleDateFormat. These terrible classes were years ago supplanted by the modern java.time classes defined in JSR 310.

Smart objects, not dumb strings

Exchange smart objects with the database rather than dumb strings. As of JDBC 4.2 we can exchange java.time objects.

The DATE type in Oracle database is akin to the SQL-standard type TIMESTAMP WITHOUT TIME ZONE. Be aware this type cannot represent a moment, cannot be used for a point in the timeline. Lacking a time zone or offset means these values can represent any moment only a range of about 26-27 hours, the range of time zones around the globe.

java.time

The equivalent type in Java is LocalDateTime.

LocalDate ld = LocalDate.of( 2021 , Month.MAY , 23 ) ;
LocalTime lt = LocalTime.of( 12 , 30 ) ;
LocalDateTime ldt = LocalDateTime.of( ld , lt ) ;

Write to database.

myPreparedStatement.setObject( … , ldt ) ;

Retrieval.

LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;

When I store a Date field in Oracle database it always shows 00:00:00 hour ¿Is it problem of SimpleDateFormat class?


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

huangapple
  • 本文由 发表于 2020年5月30日 22:45:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/62104074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定