2

I am using a JSpinner to input time; however when I use the getValue method I obtain the time on January 1st 1970, as that is the default date start. How can I get the time, and time alone? I am not interested in the date

NB: I have already made use of a dateEditor. Perhaps my JSpinnerDateModel is inappropriate?

oli.burgess
  • 87
  • 1
  • 5
  • 12
  • See the answer of this question: http://stackoverflow.com/questions/654342/is-there-any-good-and-free-date-and-time-picker-available-for-java-swing – Hernán Eche Apr 25 '12 at 14:23

1 Answers1

4

You could create a JSpinner instance and have it format a date as a time, and then just extract the time at the end:

    JSpinner jSpinner1 = new JSpinner();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date(0));
    Date earliestDate = calendar.getTime();
    calendar.add(Calendar.MINUTE, 1439); // number of minutes in a day - 1
    Date latestDate = calendar.getTime();
    SpinnerDateModel model = new SpinnerDateModel(earliestDate,
            earliestDate,
            latestDate,
            Calendar.MINUTE);
   jSpinner1.setModel(model);
   jSpinner1.setEditor(new JSpinner.DateEditor(jSpinner1, "hh:mm"));

   Date d = (Date)jSpinner1.getValue();
   Calendar c = Calendar.getInstance();
   c.setTime(d);
   c.get(Calendar.HOUR);
   c.get(Calendar.MINUTE);
tofarr
  • 7,682
  • 5
  • 22
  • 30
  • I get the first part of your suggestion... but how would one just extract the time alone? – oli.burgess Apr 01 '11 at 15:35
  • does this give you two values? how would you combine them into one value? I am taking the value and putting it into a table, hence the fact that they need to be one. – oli.burgess Apr 01 '11 at 15:52
  • If you are putting it into another field and want a single value, I suggest formatting so only the required fields are visible. - Are you displaying it in a JTable? If so there is a link here that may be useful http://www.codeguru.com/forum/showthread.php?t=40628 – tofarr Apr 01 '11 at 15:57