I am using 2 JSpinner controls in my application to allow user to input 24 hour time HH:mm format. I am trying to validate whether end time comes after start time. But it doesn't seem to work as expected. Please help me what is the right way to do this.
My spinner control looks like this:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 24);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
startModel.setValue(calendar.getTime());
JSpinner spnStartTime = new JSpinner(startModel);
JSpinner.DateEditor editor = new JSpinner.DateEditor(spnStartTime, "HH:mm");
DateFormatter formatter1 =(DateFormatter)statTimeEditor.getTextField().getFormatter();
formatter1.setAllowsInvalid(false);
formatter1.setOverwriteMode(true);
spnStartTime.setEditor(statTimeEditor);
I did the same thing for spnEndTime also.
And this is what I am trying to do now:
SimpleDateFormat parser = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
Date st=parser.parse(String.valueOf(spnStartTime.getValue()));
Date en=parser.parse(spnEndTime.getValue().toString());
if(en.after(st))
{
//do stuff
}
else
{
//display error
}
But spnStartTime.getValue() returns a date in 1970 and my editor is set to current year. So my date comparisons does not work as expected.
I tried to use st.setHours(Calendar.getInstance().getTime().getHours()); and similarly for 'en' also. But getHours() is deprecated now. So what is a proper workaround for this?