如何将Oracle Date格式转换为Java? [英] How to convert Oracle Date format to Java?

查看:63
本文介绍了如何将Oracle Date格式转换为Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Oracle Date类型以 06-MAR-20 的格式存储在数据库中,当我将其映射到Java域类时,我的属性类型为

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

保持相同吗?

还是从查询中获取数据以保持其格式时需要做一些配置吗?

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

推荐答案

日期没有特定的格式.在内部,它们以表示其年,月,日,时,分,秒的一系列字节存储.

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.

当您查看数据库中的日期时,所看到的是一个字符串表示形式,其格式取决于nls设置会话(如果已指定)或数据库(如果指定了nls_date_format 的ncode_date_format )(如果未在会话级设置该参数.

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).

如果您要以 DD-MON-YY 格式获取日期,则可以在查询中使用 to_char():

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';

这篇关于如何将Oracle Date格式转换为Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆