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.