JSpinner专门用于时间 [英] JSpinner exclusively for time

查看:106
本文介绍了JSpinner专门用于时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSpinner输入时间;但是当我使用getValue方法时,我获得了1970年1月1日的时间,因为这是默认的日期开始。我怎样才能获得时间和时间?我对日期不感兴趣

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

注意:我已经使用了dateEditor。也许我的JSpinnerDateModel不合适?

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

推荐答案

你可以创建一个JSpinner实例并让它将日期格式化为时间,然后只是在最后提取时间:

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);

这篇关于JSpinner专门用于时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆