本文介绍了将jcalendar日期转换为XMLGregorianCalendar获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,我只是从jCalendar中的jDateChooser获取空值。



此方法是将java.util.Date转换为XMlGregorianCalendar的功能:

  DatatypeFactory df; 
public XMLGregorianCalendar function_ConvertAsXMLGregorianCalendar(Date date){
if(date == null){
System.out.println(函数将日期转换成XML格里高利日历时出错);
返回null;
} else {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(date.getTime());
return df.newXMLGregorianCalendar(gc);
}
}

这是getStart和getEnd日期的2个函数。

  private XMLGregorianCalendar getStartDate(){
Date dateStarting = jDateChooserStart.getDate();
System.out.println(Date Start:+ dateStarting.toString());
XMLGregorianCalendar cal = function_ConvertAsXMLGregorianCalendar(dateStarting);
System.out.println(转换日期:+ cal.toXMLFormat());
返回cal;
}

private XMLGregorianCalendar getEndDate(){
Date dateEnding = jDateChooserEnd.getDate();
System.out.println(Date End:+ dateEnding);
return function_ConvertAsXMLGregorianCalendar(dateEnding);
}

然后我只需将方法放在一个名为schedule的对象中:

  schedule.setStartDate(getStartDate()); 
schedule.setEndDate(getEndDate());

Netbeans(v7.1)的结果

 线程中的异常AWT-EventQueue-0java.lang.NullPointerException 
在Frames.CreateSchedule.function_ConvertAsXMLGregorianCalendar(CreateSchedule.java:181)
在帧。 CreateSchedule.getStartDate(CreateSchedule.java:188)
在Frames.CreateSchedule.SubmitButtonActionPerformed(CreateSchedule.java:204)
在Frames.CreateSchedule.access $ 000(CreateSchedule.java:16)

发生了什么?



b
$ b

更新::



我只是将功能更改为:

  public XMLGregorianCalendar function_ConvertAsXMLGregorianCalendar(Date date){
if(date == null){
System.out.println(函数将日期转换为XML公历);
返回null;
} else {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(date.getTime());
DatatypeFactory df = null;
return df.newXMLGregorianCalendar(gc);
}
}

更新2#::



初始化newInstance()方法后,我会收到另一个错误:

 code> java.lang.ClassCastException:java.util.Date不能转换为java.sql.Date 

我只是将包名从

  java.util.Date更改为java.SQL.Date 

然后转换:

  Date dateStarting =(Date)jDateChooserStart.getDate(); 
日期dateEnding =(Date)jDateChooserEnd.getDate();

如何解决此问题?



再次感谢

解决方案

似乎 df 已被声明但未被实例化:

  DatatypeFactory df; 

从这里:

  df.newXMLGregorianCalendar(gc); 
^

您可以使用首先实例化,像这样: p>

  DatatypeFactory df = DatatypeFactory.newInstance(); 


i have some problem, i just getting null value from jDateChooser in jCalendar.

This method is function to convert java.util.Date into XMlGregorianCalendar :

DatatypeFactory df;
public XMLGregorianCalendar function_ConvertAsXMLGregorianCalendar(Date date) {
    if (date == null) {
        System.out.println("Error on Function Convert Date into XML Gregorian Calendar");
        return null; 
    } else {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        return df.newXMLGregorianCalendar(gc);
    }
}

And this is 2 function which getStart and getEnd Dates.

private XMLGregorianCalendar getStartDate(){
    Date dateStarting  = jDateChooserStart.getDate();
    System.out.println("Date Start : " + dateStarting.toString());
    XMLGregorianCalendar cal = function_ConvertAsXMLGregorianCalendar(dateStarting);
    System.out.println("Converted Date : " + cal.toXMLFormat());
    return cal;
}

private XMLGregorianCalendar getEndDate(){
    Date dateEnding = jDateChooserEnd.getDate();
    System.out.println("Date End : " + dateEnding);
    return function_ConvertAsXMLGregorianCalendar(dateEnding);
}

Then i just place the method inside an object called schedule:

schedule.setStartDate(getStartDate());
schedule.setEndDate(getEndDate());

Result from Netbeans(v7.1)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Frames.CreateSchedule.function_ConvertAsXMLGregorianCalendar(CreateSchedule.java:181)
at Frames.CreateSchedule.getStartDate(CreateSchedule.java:188)
at Frames.CreateSchedule.SubmitButtonActionPerformed(CreateSchedule.java:204)
at Frames.CreateSchedule.access$000(CreateSchedule.java:16)

what's wrong?

Thanks before.

UPDATE ::

I just change the function into this:

public XMLGregorianCalendar function_ConvertAsXMLGregorianCalendar(Date date) {
    if (date == null) {
        System.out.println("Error on Function Convert Date into XML Gregorian Calendar");
        return null; 
    } else {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        DatatypeFactory df = null;
        return df.newXMLGregorianCalendar(gc);
    }
}

UPDATE 2# ::

After initializing the newInstance() method, i'm getting another error:

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

I just change the package name from

java.util.Date into java.SQL.Date

then casting:

Date dateStarting  = (Date) jDateChooserStart.getDate();
Date dateEnding    = (Date) jDateChooserEnd.getDate();

How to resolve this issue?

thanks again.

解决方案

It appears that df is declared but not instantiated:

DatatypeFactory df;

from here:

df.newXMLGregorianCalendar(gc);
^

You can use DatatypeFactory.newInstance() to instantiate first, like so:

DatatypeFactory df = DatatypeFactory.newInstance(); 

这篇关于将jcalendar日期转换为XMLGregorianCalendar获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 00:48