Getting the value from JSpinner (SWING)

Klausos Klausos picture Klausos Klausos · Feb 7, 2012 · Viewed 12.7k times · Source

I need to get value from JSpinner.

private JSpinner[] timeSpinner;
private SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

Date time = new Date();
try {
  time = format.parse(timeSpinner[i].getValue().toString());
} catch (ParseException e) {
  e.printStackTrace();
}
data[i] = time.toString();
System.out.println(data[i]);

The error message:

java.text.ParseException: Unparseable date: "Thu Jan 01 01:05:00 CET 1970"

Answer

Jim picture Jim · Feb 7, 2012

The SimpleDateFormat you are creating has a different format from the date you are trying to parse:

new SimpleDateFormat("HH:mm:ss");

Thu Jan 01 01:05:00 CET 1970

Have a look at the SimpleDateFormat docs

Edit: Actually looking again it looks like you already have a Date object why not just use that?

time = (Date)timeSpinner[i].getValue();

In order to turn the date you have into a String you just have to do the above to get a Date and then do

String formattedDate = format.format(time);