Oracle date pattern to java date pattern

前端 未结 2 1434
春和景丽
春和景丽 2021-01-24 01:47

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

create or replace type type_sa         


        
相关标签:
2条回答
  • 2021-01-24 02:10

    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>
    

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

    0 讨论(0)
  • 2021-01-24 02:14

    One of the dates from the XML is 26-JAN-13. That would be a SimpleDateFormat of "dd-MMM-yy"

    SimpleDateFormat oracleFormat = new SimpleDateFormat("dd-MMM-yy");
    Date date = oracleFormat.parse(dateString);
    
    0 讨论(0)
提交回复
热议问题