Link to home
Start Free TrialLog in
Avatar of Crystalxxx
Crystalxxx

asked on

XMLGregorianCalendar to sql date in Netbeans

Does anyone know how to convert/transfer XMLGregorianCalendar to java.sql.Date?

Im using Netbeans glassfish ESB to create a patient admission service. The WSDL service is based on the xml schema(xsd). however, as im taking patient's DoB as an input, the textbox cannot convert the XMLGregorianCalendar (specified in the schema as Date) to sql (based on the connected MySQL database).

Anyone know how to solve it? Im thinkinig of using the SQL Date Converter, but really dont have a clue.

Please Help
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry
java.sql.Date d = new java.sql.Date(xgc.toGregorianCalendar().getTimeInMillis());

Open in new window

java.sql.Date d = new java.sql.Date(cal.toGregorianCalendar().getTimeInMillis());
Avatar of Crystalxxx
Crystalxxx

ASKER

Thanks a lot. But both "toGregorianCalendar()" and "getTimeInMillis()" are showing errors. Do I need do declare or import sth beforehand? The error msg is saying cannot find the method.
What code have you got already - please let's see it
should need any imports
make sure 'cal' is declared as a javax.xml.datatype.XMLGregorianCalendar
javax.xml.datatype.XMLGregorianCalendar cal = ....;
java.sql.Date d = new java.sql.Date(cal.toGregorianCalendar().getTimeInMillis());

The code is shown as following. Basically the data source is from MySQL database, field name is DoB. Here im using three input elements to retrieve the data to perform search. Familyname,Firstname and DoB. It's just the DoB that's not working. I have specified it as txtbox, rather than calendar.

package patientui;

import com.sun.rave.faces.converter.SqlDateConverter;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.PanelLayout;
import com.sun.webui.jsf.component.StaticText;
import com.sun.webui.jsf.component.TextField;
import java.sql.Date;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.faces.FacesException;
import org.netbeans.j2ee.wsdl.patientservice.patient.PatientPortType;
import org.netbeans.j2ee.wsdl.patientservice.patient.PatientService;
import org.netbeans.xml.schema.patientschema.PatDetailsReq;
import org.netbeans.xml.schema.patientschema.PatDetailsRes;

public class PatientLookUp extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private PanelLayout lpFind = new PanelLayout();

    public PanelLayout getLpFind() {
        return lpFind;
    }

    public void setLpFind(PanelLayout pl) {
        this.lpFind = pl;
    }
    private TextField txtfFN = new TextField();

    public TextField getTxtfFN() {
        return txtfFN;
    }

    public void setTxtfFN(TextField tf) {
        this.txtfFN = tf;
    }
    private TextField txtfGN = new TextField();

    public TextField getTxtfGN() {
        return txtfGN;
    }

    public void setTxtfGN(TextField tf) {
        this.txtfGN = tf;
    }
    private StaticText stmsgFN = new StaticText();

    public StaticText getStmsgFN() {
        return stmsgFN;
    }
     
    public void setStmsgFN(StaticText st) {
        this.stmsgFN = st;
    }
    private PanelLayout lpView = new PanelLayout();

    public PanelLayout getLpView() {
        return lpView;
    }

    public void setLpView(PanelLayout pl) {
        this.lpView = pl;
    }
    private Button btnSearch01 = new Button();

    public Button getBtnSearch01() {
        return btnSearch01;
    }

    public void setBtnSearch01(Button b) {
        this.btnSearch01 = b;
    }


     private TextField txtDoB = new TextField();

    public TextField getTxtDoB() {
        return txtDoB;
    }

    public void setTxtDoB(TextField tf) {
        this.txtDoB = tf;
    }




    public PatientLookUp() {
    }

 
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }


    }

   

    @Override
    public void prerender() {
        init();
        lpFind.setVisible(true);
        lpView.setVisible(false);
        stmsgFN.setVisible(false);


    }

       protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

       protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }


    public String btnLookup_action() {
        stmsgFN.setText("");
        stmsgFN.setVisible(false);
        boolean blRecordExist = false;
        PatDetailsRes patRes = null;

        try {
            PatDetailsReq msgPatDetailsReq = new PatDetailsReq();
            msgPatDetailsReq.setFamilyName((String) txtfFN.getValue());
            msgPatDetailsReq.setGivenName((String) txtfGN.getValue());
           XMLGregorianCalendar cal =txtDoB.getTime();
java.sql.Date d = new java.sql.Date(cal.toGregorianCalendar().getTimeInMillis());
               
           
            PatientService service = new PatientService();
            PatientPortType port = service.getPatientPort();
            patRes = port.getPatientDetails(msgPatDetailsReq);
            blRecordExist = patRes.isRecordExist();

I know the line "XMLGregorianCalendar cal =txtDoB.getTime();"  is not correct. It's showing error already. But im not sure how to link it to the text field i specified on the form, also the database field "DoB", which has a data type of "date"

Many thanks
compiles fine here
> I know the line "XMLGregorianCalendar cal =txtDoB.getTime();"  is not correct.

so where are you getting a XMLGregorianCalendar from?
It specified at the beginning as import javax.xml.datatype.XMLGregorianCalendar;

May I ask how you compile it? Im using Netbeans but everything is a mess
>>It's showing error already.

Please post the error
> It specified at the beginning as import javax.xml.datatype.XMLGregorianCalendar;

No, I meant where is the XMLGregorianCalendar instance you want to convert coming from, and why are you actually using it at all

> May I ask how you compile it? Im using Netbeans but everything is a mess

eclipse at the moment
Oh...i guess im lost. Didn't want to use the Calendar as input element because it's not user-friendly in case of selecting a date of birth. Because the original database im connecting and the schema declare this as date, im having problem inputting information to store in the database. I hope i explained this clearly.
>> May I ask how you compile it?

Yes - i was wondering that too - did you give objects your schema?
Please go to the command line, change to the root directory of your project and run the following, and afterwards attach the file 'comp.txt' from the same directory. You can delete it afterwards
ant -logfile comp.txt compile 

Open in new window

looks like you just need something like the following to parse the date, then you can instert the date in the database

http://helpdesk.objects.com.au/java/how-do-i-parse-a-java-string-that-specifies-a-date (change the format to suit your needs)
Thanks guys this code now works,
XMLGregorianCalendar cal = msgPatDetailsReq.getDoB();
java.sql.Date d = new java.sql.Date(cal.toGregorianCalendar().getTimeInMillis());

But im wondering how can i link it bk to the textbox again? What i did by using the code above was the date conversion, does anyone know  how to retrieve the information afterwards? The textbox name is txtDoB

Thanks
However, the date above has given me a LONG format
I think the problem is because im using "getTimeInMillis()“. Is there any other way?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use the following to format a date in short format:
String sDate = String.format("%td/%<tm/%<ty", date);

Open in new window