JSpinner exclusively for time

a 夏天 提交于 2019-12-02 01:49:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!