0

I have created a Java Swing interface which has 4 components

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 

All the final dates must be SQL Date and SQL Time and not java.util

I am trying to create two different dates: starting date and ending date

Here the code:

        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

                    }

I would like to create two dates and compare them.

Many methods are deprecated and I think the solution is very easy

Thank you

mKorbel
  • 109,525
  • 20
  • 134
  • 319
QGA
  • 3,114
  • 7
  • 39
  • 62

2 Answers2

0

Try this.

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
}

If you want the date and time format, then you can try.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:ss:ii");
jpr
  • 185
  • 3
  • 6
  • 20
  • My problem is about retriving date and time from two different components and create a date variable which has the date from the first component in the "yyyy-MM-dd" format and the time of the second component in the 00-00-00 format. Afterwards I need to do the same process for the last two components and then compare the two created dates. – QGA Mar 06 '14 at 11:34
  • You can use finishingDate.getTime() to get the time of second component. It will return time in milliseconds from 1-Jan-1970, but for comparison this would be good enough. – jpr Mar 06 '14 at 12:02
  • I need to use the created dates later to query a sql database, see what I did in my answer to solve it – QGA Mar 06 '14 at 12:08
0

I solved it in this way

    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
                    }
QGA
  • 3,114
  • 7
  • 39
  • 62