Results 1 to 10 of 10
  1. #1
    habiler is offline Member
    Join Date
    Sep 2016
    Posts
    44
    Rep Power
    0

    Default setDate - Différence bt two dates..

    Hi the community.

    What's wrong with my code. I try to determine the number of months between two dates but the code indicates an error at setDate
    Who can help me?
    Thanks

    Habiler

    Java Code:
                  Calendar sDate = Calendar.getInstance();
                            Calendar eDate = Calendar.getInstance();
                            sDate.setDate(jDateChooserFin.getDate());
                            eDate.setDate(jDateChooserDebut.getDate());
                            int difInMonths = sDate.get(Calendar.MONTH) - eDate.get(Calendar.MONTH);
                            JOptionPane.showMessageDialog(null,""+difInMonths+" mois","Nombre de mois !!!",JOptionPane.PLAIN_MESSAGE);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: setDate - Différence bt two dates..

    the code indicates an error at setDate
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    habiler is offline Member
    Join Date
    Sep 2016
    Posts
    44
    Rep Power
    0

    Default Re: setDate - Différence bt two dates..

    Here is the code of errors

    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.Calendar.setDate
    at decisions.AddDecisionsJFrame1$7.propertyChange(Add DecisionsJFrame1.java:311)
    at java.beans.PropertyChangeSupport.fire(PropertyChan geSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:327)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:263)
    at java.awt.Component.firePropertyChange(Component.ja va:8428)
    at com.toedter.calendar.JDateChooser.propertyChange(U nknown Source)
    at java.beans.PropertyChangeSupport.fire(PropertyChan geSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:328)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:263)
    at java.awt.Component.firePropertyChange(Component.ja va:8428)
    at com.toedter.calendar.JTextFieldDateEditor.setDate( Unknown Source)
    at com.toedter.calendar.JTextFieldDateEditor.setDate( Unknown Source)
    at com.toedter.calendar.JDateChooser.setDate(Unknown Source)
    at com.toedter.calendar.JDateChooser.propertyChange(U nknown Source)
    at java.beans.PropertyChangeSupport.fire(PropertyChan geSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:328)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:263)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:283)
    at java.awt.Component.firePropertyChange(Component.ja va:8468)
    at javax.swing.JComponent.firePropertyChange(JCompone nt.java:4507)
    at com.toedter.calendar.JDayChooser.setDay(Unknown Source)
    at com.toedter.calendar.JDayChooser.actionPerformed(U nknown Source)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6533)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3324)
    at java.awt.Component.processEvent(Component.java:629 8)
    at java.awt.Container.processEvent(Container.java:223 6)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4889)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2294)
    at java.awt.Component.dispatchEvent(Component.java:47 11)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746 )
    at java.awt.Component.dispatchEvent(Component.java:47 11)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 76)
    at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 728)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:82)
    BUILD SUCCESSFUL (total time: 29 seconds)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: setDate - Différence bt two dates..

    Can you use the javac.exe command to compile your code and get its error message?

    Uncompilable source code - Erroneous sym type: java.util.Calendar.setDate
    I don't recognize the message that your IDE is showing when the code is being executed.

    Can you copy the API doc for the setDate() method and paste it here?
    Last edited by Norm; 09-19-2016 at 10:50 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: setDate - Différence bt two dates..

    It would also be good to see the import of the external package plus the instantiation: Chances are that the external Toedter package was not properly added to the project.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: setDate - Différence bt two dates..

    Don't try and run code that does not compile.
    That message is telling you the code has errors, so those need fixing first.

    So what error is the IDE saying is wrong with those lines?
    Copy and paste it here.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    habiler is offline Member
    Join Date
    Sep 2016
    Posts
    44
    Rep Power
    0

    Default Re: setDate - Différence bt two dates..

    Hello to all,

    I haved tried another code : but i still have an error :



    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import javax.swing.*;
    import java.util.*;
    .
    .
    .



    {

    Date depart = (Date) jDateChooserFin.getDate();
    Date retour= (Date) jDateChooserDebut.getDate();
    long dureest= (depart.getTime()- retour.getTime())/86400000 ;
    JOptionPane.showMessageDialog(null, dureest);
    }
    Error :
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
    bon d'accord ...
    at decisions.AddDecisionsJFrame$7.propertyChange(AddD ecisionsJFrame.java:387)
    at java.beans.PropertyChangeSupport.fire(PropertyChan geSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:327)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:263)
    at java.awt.Component.firePropertyChange(Component.ja va:8428)
    at com.toedter.calendar.JDateChooser.propertyChange(U nknown Source)
    at java.beans.PropertyChangeSupport.fire(PropertyChan geSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:328)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:263)
    at java.awt.Component.firePropertyChange(Component.ja va:8428)
    at com.toedter.calendar.JTextFieldDateEditor.setDate( Unknown Source)
    at com.toedter.calendar.JTextFieldDateEditor.setDate( Unknown Source)
    at com.toedter.calendar.JDateChooser.setDate(Unknown Source)
    at com.toedter.calendar.JDateChooser.propertyChange(U nknown Source)
    at java.beans.PropertyChangeSupport.fire(PropertyChan geSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:328)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:263)
    at java.beans.PropertyChangeSupport.firePropertyChang e(PropertyChangeSupport.java:283)
    at java.awt.Component.firePropertyChange(Component.ja va:8468)
    at javax.swing.JComponent.firePropertyChange(JCompone nt.java:4507)
    at com.toedter.calendar.JDayChooser.setDay(Unknown Source)
    at com.toedter.calendar.JDayChooser.actionPerformed(U nknown Source)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6533)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3324)
    at java.awt.Component.processEvent(Component.java:629 8)
    at java.awt.Container.processEvent(Container.java:223 6)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4889)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2294)
    at java.awt.Component.dispatchEvent(Component.java:47 11)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746 )
    at java.awt.Component.dispatchEvent(Component.java:47 11)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 76)
    at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessI mpl.doIntersectionPrivilege(ProtectionDomain.java: 76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 728)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:82)
    BUILD SUCCESSFUL (total time: 12 seconds)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: setDate - Différence bt two dates..

    You are importing java.sql.Date, so any Date in your code is one of them.
    The Date returned from the jDateChoosers is a java.util.Date.
    You can't cast that to a sql.Date.

    Why are you using java.sql.Date?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    habiler is offline Member
    Join Date
    Sep 2016
    Posts
    44
    Rep Power
    0

    Default Re: setDate - Différence bt two dates..

    Quote Originally Posted by Tolls View Post
    You are importing java.sql.Date, so any Date in your code is one of them.
    The Date returned from the jDateChoosers is a java.util.Date.
    You can't cast that to a sql.Date.

    Why are you using java.sql.Date?
    Thank you.

    Sorry but is it possible to define pecisely the number of monthd
    s os better the number of years months and days.
    a methode does exist for so a solution ?

    I m a new java programmer and i am very patauging in the classes methods

    Thanks

    Habiler.

  10. #10
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: setDate - Différence bt two dates..

    Obviously I don't know the overall purpose of your program, but if the program is only about doing calculations with time or date, I would throw overboard all date and calendar packages that you have so far and move them into the Java.time API that came with Java 8. You can do all kinds of calculations there.

    Here are just a few examples.

Similar Threads

  1. Difference in days between dates
    By peter_62au in forum New To Java
    Replies: 17
    Last Post: 06-11-2014, 04:49 PM
  2. Difference between 2 dates using Scanner
    By martinhocosta88 in forum New To Java
    Replies: 3
    Last Post: 10-26-2013, 03:39 AM
  3. difference between two dates..
    By Shahnawaz in forum New To Java
    Replies: 2
    Last Post: 09-07-2011, 07:35 AM
  4. Strings as Dates and difference
    By kryptonian03 in forum New To Java
    Replies: 7
    Last Post: 03-21-2011, 01:35 AM
  5. Difference of Two Dates
    By jboy in forum New To Java
    Replies: 3
    Last Post: 09-15-2009, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •