XMLGregorianCalendar to sql date in Netbeans

Crystalxxx
Crystalxxx used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Try
java.sql.Date d = new javasql.Date(xgc.toGregorianCalendar().getTimeInMillis());

Open in new window

Top Expert 2016

Commented:
Sorry
java.sql.Date d = new java.sql.Date(xgc.toGregorianCalendar().getTimeInMillis());

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
java.sql.Date d = new java.sql.Date(cal.toGregorianCalendar().getTimeInMillis());
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
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.
Top Expert 2016

Commented:
What code have you got already - please let's see it
Mick BarryJava Developer
Top Expert 2010

Commented:
should need any imports
make sure 'cal' is declared as a javax.xml.datatype.XMLGregorianCalendar
Mick BarryJava Developer
Top Expert 2010

Commented:
javax.xml.datatype.XMLGregorianCalendar cal = ....;
java.sql.Date d = new java.sql.Date(cal.toGregorianCalendar().getTimeInMillis());

Author

Commented:
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();

Author

Commented:
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
Mick BarryJava Developer
Top Expert 2010

Commented:
compiles fine here
Mick BarryJava Developer
Top Expert 2010

Commented:
> I know the line "XMLGregorianCalendar cal =txtDoB.getTime();"  is not correct.

so where are you getting a XMLGregorianCalendar from?

Author

Commented:
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
Top Expert 2016

Commented:
>>It's showing error already.

Please post the error
Mick BarryJava Developer
Top Expert 2010

Commented:
> 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

Author

Commented:
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.
Top Expert 2016

Commented:
>> May I ask how you compile it?

Yes - i was wondering that too - did you give objects your schema?
Top Expert 2016

Commented:
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

Mick BarryJava Developer
Top Expert 2010

Commented:
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)

Author

Commented:
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

Author

Commented:
However, the date above has given me a LONG format

Author

Commented:
I think the problem is because im using "getTimeInMillis()“. Is there any other way?
Mick BarryJava Developer
Top Expert 2010
Commented:
following shows you how to format the Date as a string

http://helpdesk.objects.com.au/java/how-do-i-format-a-date-as-a-string
Top Expert 2016

Commented:
You can use the following to format a date in short format:
String sDate = String.format("%td/%<tm/%<ty", date);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial