JSpinner exclusively for time

浪尽此生 提交于 2019-12-02 02:21:35

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