繁体   English   中英

自动增加SWT日期时间的日期

[英]Increase the date of a SWT Datetime automatically

在我的程序中,我有两个org.eclipse.swt.widgets.DateTime小部件。 如果更改了一个(天,月或年),则另一个应设置为同一日期,增加两天。

例如,一个被设置为01.01.2014 ,另一个应跳到03.01.2014

有没有解决的办法,或者您知道有关这方面的摘要吗? 我不想总是检查第一个DateTime是否设置为一个月末,以便第二个应该跳至01.02.2014 1月2日或02.02.2014 2月2日...

我希望你理解我在寻找什么;)

您可以使用java.util.Calendar ,尤其可以使用add(int, int)方法将两个添加到Calendar.DAY_OF_YEAR 它将自动移至下个月/年:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell();
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(2, false));

    final DateTime first = new DateTime(shell, SWT.CALENDAR);
    final DateTime second = new DateTime(shell, SWT.CALENDAR);

    first.addListener(SWT.Selection, new Listener()
    {
        private Calendar cal = Calendar.getInstance();

        @Override
        public void handleEvent(Event arg0)
        {
            cal.set(Calendar.YEAR, first.getYear());
            cal.set(Calendar.MONTH, first.getMonth());
            cal.set(Calendar.DAY_OF_MONTH, first.getDay());

            cal.add(Calendar.DAY_OF_YEAR, 2);

            second.setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
        }
    });

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

使用java.util.Calendar

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 2);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM