How to convert Oracle Date format to Java? [duplicate]

不想你离开。 提交于 2021-02-11 15:13:23

问题


I have an Oracle Date type stored in the db in this format 06-MAR-20, when i mapped it to a java domain class i had a property of type

java.SQL.Date

but i got the data in this format

2020-03-06

How can i stick to the exact same format from whats stored in my db? Will

java.SQL.Timestamp

keep it the same?

Or do i need to do some configuration when getting the data from query to maintain its format?


回答1:


Dates do not have a specific format. Internally, they are stored as a series of bytes that represent its year, month, day, hour, minutes, seconds.

What you are seeing when you look at your date in the database is a string representation whose format depends on nls setting nls_date_format of either your session (if it specified one), or of your database (if the parameter was not set at session level).

If you want to fetch your date in format DD-MON-YY, you can either use to_char() in your query:

to_char(mydatecol, 'DD-MON-YY')

Or you can set it at session level:

alter session set nls_date_format = 'DD-MON-YY';



回答2:


tl;dr

I have an Oracle Date type stored in the db in this format 06-MAR-20

No, you don’t.

  • Databases such as Oracle store date-time values with their own internally defined binary values, not as plain text.
  • Oracle DATE type holds a time-of-day as well as a date.

DATE = date + time-of-day

The DATE type in Oracle database is misnamed. It holds more than a date (year, month, and day-of-month).

This type represents a date with time-of-day resolving to whole seconds. This type lacks the context of a time zone or offset-from-UTC. So this type cannot be used to represent a moment, a specific point on the timeline. (To track a moment, use Oracle type TIMESTAMP WITH TIME ZONE.)

Note that this Oracle naming differs from the SQL standard. In the standard, a DATE type is a date-only value, with no time-of-day and no time zone or offset.

java.time.LocalDateTime

So this type maps to LocalDateTime in Java.

Retrieval.

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

Storage.

myPreparedStatement.setObject( … , ldt ) ;

Text

Notice that in the code above we are using smart objects rather than dumb strings.

Date-time objects such as LocalDateTime are not String and do not hold text. They internally represent their value in their own way. You can parse a string as a date-time object, and you can ask the date-time object to generate text. But the text and the date-time object are separate and distinct.

The java.time classes use standard ISO 8601 formats when parsing/generating strings.

String output = ldt.toString() ;

2020-01-23T01:23:45.123456789

Parsing.

You can generate text in other formats. You can specify your own custom format by calling DateTimeFormatter.ofPattern. Usually better to automatically format by calling DateTimeFormatter.ofLocalized… methods.

This has been covered many many times already on Stack Overflow. So search to learn more.

LocalDateTime ldt = LocalDateTime.parse( "2020-01-23T01:23:45.123456789" ) ;

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?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….



回答3:


I felt like you did not search the stackoverflow properly. There are lots of answers related to the problem you mentioned here. That will give you better idea how to tackle the problem.

The above answer can only be used if you are using straight JDBC, but usually enterprise apps will use some ORMs and most of then use it along with spring. Then you may not get the freedom to correct the sql everywhere and writing sql for every domain class is not recommended. So it is better to write custom methods for date properties that you have in your domain to get the date string in whatever format you want. Sample getStringDate(java.SQL.Date) to convert it to the format you want.

Date sqldate = new Date(new java.util.Date().getTime());
System.out.println(sqldate);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yy");
System.out.println(format.format(sqldate));


来源:https://stackoverflow.com/questions/61279889/how-to-convert-oracle-date-format-to-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!