比较各种组件的SQL日期

时间:2014-03-06 10:37:48

标签: java swing datetime swingx jspinner

我创建了一个Java Swing接口,它有4个组件

spinner      (JXDatePicker)    //get only date in this format yyyy-MM-dd

spinner_1    (JTimeChooser)    //get only time in this format 00-00-00  

spinner_2    (JXDatePicker)    //get only date in this format yyyy-MM-dd

spinner_3    (JTimeChooser)    //get only time in this format 00-00-00 

所有最终日期必须是SQL Date和SQL Time,而不是java.util

我正在尝试创建两个不同的日期:开始日期和结束日期

这里是代码:

        Calendar startingDate = Calendar.getInstance();
        startingDate.setTime(spinner.getDate());

        //This is deprecated
        //Date dt = new Date(spinner.getDate().getTime());
        //dt.getHours();
        //dt.getMinutes();
        //dt.getSeconds();

        Calendar startingDateTime = Calendar.getInstance();
                    //this is an example but it does not point to the right year,month and day
        startingDate.set(startingDate.YEAR, startingDate.MONTH, startingDate.DAY_OF_MONTH, spinner_1.getHours(), spinner_1.getMinutes(), spinner_1.getSeconds());;

        Calendar finishingDate = Calendar.getInstance();
        finishingDate.setTime( spinner_2.getDate());

        Calendar finishingDateTime = Calendar.getInstance();
                    //Same of above
        finishingDateTime.set(finishingDate.YEAR, finishingDate.MONTH, finishingDate.DAY_OF_MONTH, spinner_3.getHours(), spinner_3.getMinutes(), spinner_3.getSeconds());;

        if(startingDateTime.equals(finishingDateTime)){
            JOptionPane.showMessageDialog(null,
                    "Please insert two different dates.",
                    "Date Error",
                    JOptionPane.ERROR_MESSAGE);
                return;
        }else if(startingDateTime.after(finishingDateTime)){
            JOptionPane.showMessageDialog(null,
                    "Please the ending date must be after the starting date.",
                    "Date Error",
                    JOptionPane.ERROR_MESSAGE);
                return;             
        }else{

                    //do something

                    }

我想创建两个日期并进行比较。

许多方法已被弃用,我认为解决方案非常简单

谢谢

2 个答案:

答案 0 :(得分:0)

试试这个。

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date startingDate = dateFormat.parse(spinner.getDate());
java.util.Date finishingDate = dateFormat.parse(spinner_2.getDate());
if(enDate.before(stDate)) {
    //do something
} else {
    //do something else
}

如果您想要日期和时间格式,那么您可以尝试。

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:ss:ii");

答案 1 :(得分:0)

我用这种方式解决了它

    Calendar startingDate = Calendar.getInstance();
        startingDate.setTime(spinner.getDate());            
        startingDate.set(Calendar.HOUR_OF_DAY,spinner_1.getHours());
        startingDate.set(Calendar.MINUTE,spinner_1.getMinutes());
        startingDate.set(Calendar.SECOND,spinner_1.getSeconds());


        Calendar finishingDate = Calendar.getInstance();
        finishingDate.setTime( spinner_2.getDate());            
        finishingDate.set(Calendar.HOUR_OF_DAY,spinner_3.getHours());
        finishingDate.set(Calendar.MINUTE,spinner_3.getMinutes());
        finishingDate.set(Calendar.SECOND,spinner_3.getSeconds());


        if(startingDate.equals(finishingDate)){
            JOptionPane.showMessageDialog(null,
                    "Please insert two different dates.",
                    "Date Error",
                    JOptionPane.ERROR_MESSAGE);
                return;
        }else if(startingDate.after(finishingDate)){
            JOptionPane.showMessageDialog(null,
                    "Please the ending date must be after the starting date.",
                    "Date Error",
                    JOptionPane.ERROR_MESSAGE);
                return;             
        }else{
                    //do something
                    }