问题
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?
回答1:
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);
来源:https://stackoverflow.com/questions/5515413/jspinner-exclusively-for-time