繁体   English   中英

Oracle日期模式到Java日期模式

[英]Oracle date pattern to java date pattern

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

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来自数据库。 在这里,日期格式根据运行上述代码的会话的不同格式而不同。

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


我在这里没有得到预期的答案。 可能是我的演示文稿没有清楚地解释问题。

我现在将直接提出一个问题。 据我所知,java.text.SimpleDateFormat毕竟是java.text.DateFormat的实现。 DateFormat的此实现可以理解以下模式字母

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实现可以理解其他模式字母组吗? 如果有的话,请告诉我。

XML的日期之一是13-JAN-13。 那将是“ dd-MMM-yy”的SimpleDateFormat

SimpleDateFormat oracleFormat = new SimpleDateFormat("dd-MMM-yy");
Date date = oracleFormat.parse(dateString);

并不是您要问的那样,但是这是一种避免会话NLS参数不确定性的方法。 您可以在对象中进行转换,因此可以使用显式的日期格式掩码,当将其转换为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;
/

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

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 ),日期在XML中始终采用YYYY-MM-DD格式。 当然,您可以使用任何想要的固定格式,只需要在对象和Java之间达成一致即可。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM