简体   繁体   English

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

[英]Increase the date of a SWT Datetime automatically

In my program I have two org.eclipse.swt.widgets.DateTime widgets. 在我的程序中,我有两个org.eclipse.swt.widgets.DateTime小部件。 If the one is changed (day, month or year) the other one should be set to the same date increased by two days. 如果更改了一个(天,月或年),则另一个应设置为同一日期,增加两天。

For example the one is set to 01.01.2014 the other one should jump to 03.01.2014 . 例如,一个被设置为01.01.2014 ,另一个应跳到03.01.2014

Is there any solution, or do you know any snippets about that? 有没有解决的办法,或者您知道有关这方面的摘要吗? I don't want to check always if the first DateTime is set to the end of a month, so that the second one should jump to 01.02.2014 or 02.02.2014 ... 我不想总是检查第一个DateTime是否设置为一个月末,以便第二个应该跳至01.02.2014 1月2日或02.02.2014 2月2日...

I hope you understand what I am searching for ;) 我希望你理解我在寻找什么;)

You can use java.util.Calendar and in particular the method add(int, int) to add two to Calendar.DAY_OF_YEAR . 您可以使用java.util.Calendar ,尤其可以使用add(int, int)方法将两个添加到Calendar.DAY_OF_YEAR It will move to the next month/year automatically: 它将自动移至下个月/年:

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

Use java.util.Calendar : 使用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