0

I used JSpinner to set time (hours and minutes with pattern "HH:mm").

I initialize JSpinner as below:

JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerDateModel());
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(spinner, "HH:mm");
spinner.setEditor(timeEditor);
spinner.setValue(new Date());

And when I get value

(Date) spinner.getValue()

without changing values - I get correct value Tue Mar 14 14:46:41 CET 2017, but when I change one minutes down It return the "epoche" date as Thu Jan 01 14:45:00 CET 1970. "Epoche" date means that the time (hours and minutes) are ok but date year, month or day are wrong.

How to initialize the JSpinner to always get actual date by spinner.getValue()? - not epoche date

EDIT

I found solution as below

Date spinnerValue = (Date) spinner.getValue();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MMM/dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
Date actualDate = sdf2.parse(sdf1.format(new Date()) + " " + sdf.format(spinnerValue), new ParsePosition(0));

... it do not looks good, I want write it smaller and smarter.

ACz
  • 567
  • 5
  • 22

1 Answers1

0

The set value and the range of possible values are not bound.

Set step for a DateEditor in a JSpinner

You have to define whats the minimum value of the spinner and whats the maximum value, so the spinner knows what a spinner-tick is pointing to and how much time is left when going to the next tick.

The first getValue command will return the previous set Date(), which was initialized by yourself.

Further values are generated by the DateEditor and this one has to be initialized also by yourself. If not you will get default values from this DateEditor.

Community
  • 1
  • 1
Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
  • So how to get correct (acutal) date by getting changed value from spinner ? – ACz Mar 14 '17 at 14:24
  • What date the spinner is pointing to when the minimum is selected (left)? Today, yesterday, 1970? And is a tick changing hours, minutes, years, milliseconds... – Markus Lausberg Mar 14 '17 at 14:26
  • I changed only hours and minutes, as a pattern defines. But I need to get full date which only hours and mintues changed. I edit my post, check what I want to achieve. – ACz Mar 14 '17 at 14:28