如何从JDateChooser获得日期和格式?



如何从JDateChooser获取日期并格式化它?

JDateChooser

private void jDateChooser1PropertyChange(java.beans.PropertyChangeEvent evt) { 

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(jDateChooser1.getDate());
System.out.println("date:"+ date);

}  

我得到如下所示的错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke 
"java.util.Date.getTime()" because "date" is null
at java.base/java.util.Calendar.setTime(Calendar.java:1801)
at java.base/java.text.SimpleDateFormat.format(SimpleDateFormat.java:975)
at java.base/java.text.SimpleDateFormat.format(SimpleDateFormat.java:968)
at java.base/java.text.DateFormat.format(DateFormat.java:375)
at com.mycompany.pos.add_stock_adjustment1.jDateChooser1PropertyChange(add_stock_adjustment1.java:880)
at com.mycompany.pos.add_stock_adjustment1$7.propertyChange(add_stock_adjustment1.java:466)
at java.desktop/java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:341)
at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:333)
at java.desktop/java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:266)    

如何修复此错误?

private void jDateChooser1PropertyChange(java.beans.PropertyChangeEvent evt) {                                             
if ("date".equals(evt.getPropertyName())) { 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(jDateChooser1.getDate());
System.out.println("date:"+ date);
}else{
System.out.println("null");
} 
} 

现在可以用了

相关内容

  • 没有找到相关文章

最新更新