Results 1 to 5 of 5
  1. #1
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default SpinnerDateModel questions

    In my application user needs to input date. I read the Java API and found that I can solve this with the following code

    Java Code:
    dataReceivedDateSpinner = new JSpinner(new SpinnerDateModel());

    I have two problems.

    First: dataReceivedDateSpinner displays year, month, day, hour and minutes. And it is displaying it as far as I can tell according to system time settings. On my home computer which runs Linux format is 4/30/10 9:10 PM. At work where we use Serbian Windows settings format is petak 30.4.2010 21:10. Is there a way for me to set the format to let say DD/MM/YYYY. I would like system independent formating. Plus I don't need the time just the date. Is there a way to at least turn the time off and is there a way to tell the JSpinner or SpinnerDateModel in which format I want data to be displayed.

    Second: I can get the Date from dataReceivedDateSpinner and set value in the following fashion

    Java Code:
    Date date = (Date) dataReceivedDateSpinner.getModel().getValue();
    dataReceivedDateSpinner.getModel().setValue(date);

    Problem is that, as far as I understood Java api, most methods of Date class are deprecated. Recommendation is not to use them. I have fields in my classes that I want to use for dates and those fields are instances of GregorianCalendar. I googled extensively but found no way to switch back and forth betwen GregorianCalendar class and Date class.

    Is there a way to use some up to date class to store date information and convert data from Date class to that up to date class and back or do I have to use deprecated methods of the Date class. I would really like to avoid using Date class methods because they are deprecated.

    Thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For your first problem, I think that you can solve it by giving your JSpinner a custom JSpinner.DateEditor editor. Please check out the API here: JSpinner.DateEditor (Java Platform SE 6)

    There's a constructor that takes a JSpinner and Date format pattern String as parameters which will likely solve your problem. Then use JSpinner's setEditor method to set the editor with your object.

  3. #3
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default

    Thank you. I solved first problem with

    Java Code:
    dataReceivedDateSpinner = new JSpinner(new SpinnerDateModel());
    dataReceivedDateSpinner.setEditor(new JSpinner.DateEditor(dataReceivedDateSpinner, TypeDefinitions.DATE_FORMAT));

    Where TypeDefinitions.DATE_FORMAT is "dd.MM.yyyy". I set editors to all JSpinners that are used for date input like this.

    Does anybody know any solution for my second problem? Is working with Date class all the way the only way to get and set value to JSpinner created in the above fashion.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by frenk_castle View Post
    I have fields in my classes that I want to use for dates and those fields are instances of GregorianCalendar. I googled extensively but found no way to switch back and forth betwen GregorianCalendar class and Date class.
    Read the API for GregorianCalendar and don't miss the methods inherited from Calendar. Look for methods that return a Date or accept a Date as parameter and you can't miss.

    db

  5. #5
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default

    I don't know how to thank you enough. I must have read GregorianCalendar API 15 times. It was very strange to me that there were no methods that will enable GregorianCalendar Class to work with Date class expecially considering Date class was deprecated . Somehow I managed to miss those methods.

    Thank you.

Similar Threads

  1. Some Questions
    By MuslimCoder in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 04:01 PM
  2. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 01:13 AM
  3. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM
  4. questions
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-27-2007, 11:18 PM
  5. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 04:57 AM

Posting Permissions

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