Oracle 日期模式到 java 日期模式 [英] Oracle date pattern to java date pattern

查看:29
本文介绍了Oracle 日期模式到 java 日期模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何实用程序可以从 Oracle to_char 格式模式返回 java.text.SimpleDateFormat 模式?

Is there any utility that returns java.text.SimpleDateFormat pattern from Oracle to_char format pattern?

create or replace type type_sample as object (
  f_id number,
  f_name varchar2(100),
  f_start_date date,
  f_end_date date
)
/

SET SERVEROUTPUT ON
DECLARE
  xmltype1            SYS.XMLType;
  message             type_sample;
BEGIN
  message := new type_sample(1, 'Manohar', current_date, sysdate);
  xmltype1 := XMLtype.createXML(message);
  DBMS_OUTPUT.PUT_LINE('out_errm : '|| xmltype1.getStringVal());
END;
/

<TYPE_SAMPLE><F_ID>1</F_ID><F_NAME>Manohar</F_NAME><F_START_DATE>26-JAN-13</F_START_DATE>**<F_END_DATE>26-JAN-13</F_END_DATE>**</TYPE_SAMPLE>

我有来自数据库的上述 XML.这里的日期格式根据上面代码运行的会话采用不同的格式.

I have the above XML coming form DataBase. Here the date format is coming in different formats based on the session the above code is run.

我在这里可以做的是,可以从 Java 端获取该会话的日期格式模式.如果我可以将该日期格式模式(oracle db)转换为 java.text.SimpleDateFormat 模式,我的问题就解决了.你能帮我吗?

What I can do here is, can get that session's dateformat pattern from to java side. If I can convert that dateformat pattern(oracle db) to java.text.SimpleDateFormat pattern, my problem is solved. Can you please help me out?

我在这里没有得到预期的答案.可能是我的演示没有清楚地说明问题.

I am not getting the expected answer here. May be my presentation is not explaining the problem clearly.

我现在会问一个直截了当的问题.据我所知 java.text.SimpleDateFormat 毕竟是 java.text.DateFormat 的一个实现.DateFormat的这个实现可以理解下面的模式字母

I will ask a straight forward question now. As far as i know java.text.SimpleDateFormat is after all an implementation of java.text.DateFormat. This implementation of DateFormat can understand the following pattern letters

G   Era designator
y   Year
M   Month in year
w   Week in year
W   Week in month
D   Day in year
d   Day in month
F   Day of week in month
E   Day in week
a   Am/pm marker
H   Hour in day (0-23)
k   Hour in day (1-24)
K   Hour in am/pm (0-11)
h   Hour in am/pm (1-12)
m   Minute in hour
s   Second in minute
S   Millisecond
z   Time zone
Z   Time zone 

java.text.DateFormat 有没有其他的实现,可以理解其他一些模式字母组?如果有,请告诉我.

Are there any other implementations of java.text.DateFormat, which can understand some other pattern letter group? If any, please let me know.

推荐答案

不是您要问的那样,但这里有一种不同的方法可以避免会话 NLS 参数的不确定性.您可以在对象中进行转换,因此您可以使用显式日期格式掩码,并且在将其转换为 XML 时它已经是一个字符串:

Not quite what you were asking, but here's a different approach to avoid the uncertainty from the session's NLS parameters. You could make the conversion happen in the object, so you can use an explicit date format mask and it's already a string when you convert it to XML:

create or replace type type_sample as object (
    f_id number,
    f_name varchar2(100),
    f_start_date varchar2(10),
    f_end_date varchar2(10),
    constructor function type_sample(self in out nocopy type_sample,
        f_id number, f_name varchar2, f_start_date date, f_end_date date)
        return self as result
)
/

create or replace type body type_sample as
    constructor function type_sample(self in out nocopy type_sample,
        f_id number, f_name varchar2, f_start_date date, f_end_date date)
        return self as result is
    begin
        self.f_id := f_id;
        self.f_name := f_name;
        self.f_start_date := to_char(f_start_date, 'YYYY-MM-DD');
        self.f_end_date := to_char(f_end_date, 'YYYY-MM-DD');
        return;
    end;
end;
/

创建对象的方式没有变化:

There's no change to how you create the object:

set serveroutput on
declare
    xmltype1 sys.xmltype;
    message type_sample;
begin
    message := new type_sample(1, 'Manohar', current_date, sysdate);
    xmltype1 := xmltype.createxml(message);
    dbms_output.put_line('out_errm : '|| xmltype1.getStringVal());
end;
/

out_errm : <TYPE_SAMPLE><F_ID>1</F_ID><F_NAME>Manohar</F_NAME><F_START_DATE>2013-02-04</F_START_DATE><F_END_DATE>2013-02-04</F_END_DATE></TYPE_SAMPLE>

无论会话的 NLS_DATE_FORMAT(恰好是 DD-MON-RR 在这个会话中).您当然可以使用任何您想要的固定格式,您只需要在对象和 Java 之间同意该格式即可.

The date is always in YYYY-MM-DD format in the XML, regardless of the session's NLS_DATE_FORMAT (which happens to be DD-MON-RR in this session). You could use whatever fixed format you want of course, you just need to agree that format between the object and Java.

(我想我假设这是该对象唯一的用途;否则将日期放入字符串不是一个好主意......).

(I suppose I'm assuming this is the only thing the object will be used for; otherwise putting dates into strings isn't a good idea...).

这篇关于Oracle 日期模式到 java 日期模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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