-1

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?

developer3
  • 75
  • 2
  • 9
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 29 '16 at 09:56
  • On a shorter note, how do i compare the (HH:mm)time values obtained as user input from 2 JSpinner controls? And how do I retrieve just the hours and minutes part from a Date object. Any help on these would be of great value to me. – developer3 Nov 29 '16 at 10:28
  • last time I posted a different question, people commented that I should post a more detailed code if someone should help me. So this time I posted the details and I only see downvotes. What exactly should I post?!!! – developer3 Nov 30 '16 at 05:39
  • *"What exactly should I post?!!!"* What exactly is it about the first two links that you do not understand after carefully reading them? *"last time I posted a different question"* Not prepared to comment on other questions that you don't link to. In fact, all that is relevant is right here. – Andrew Thompson Nov 30 '16 at 07:23

1 Answers1

0

On your calendar object you are only setting the time but not the date. Therefore the date on your model is unchanged: 1970-01-01. Also set the year, month and day. You can also take a look at How to use Spinners.

hotzst
  • 7,238
  • 9
  • 41
  • 64
  • From what I understood, when I call calender=Calendar.getInstance(); it will set the current date and time to calender. Isn't that so? Also, when I tried to print the value of calender.getTime() it was giving me the current year and month. – developer3 Nov 29 '16 at 10:23