Mahir Duraković
Mahir Duraković

Reputation: 135

How to get only year, month,day,horus and seconds from Java JSpinner

I am working on my MySQL project and I have one problem. I have made one JSpinner that shows current date and time. Now I want to save that date and time in one datetime variable for later use, but problem is when I save value from JSpinner into my datetime variable its not in 'YYYY-MM-DD HH:MM:SS' format. Instead of that my datetime variable contains this: 'Sat Aug 17 19:07:03 CEST 2013'. I want to save only numbers in classic 'YYYY-MM-DD HH:MM:SS' format because my database on my server accepts only data in that format, but I dont know how to cast it. Please help!

Here is my code:

String datum_vrijeme;
SpinnerDateModel s1 = new SpinnerDateModel();
spinner1 = new JSpinner(s1);
JSpinner.DateEditor d1 = new JSpinner.DateEditor(spinner1, "yyyy/MM/dd   HH:mm:ss");
spinner1.setEditor(d1);
Object sp = spinner1.getValue();
datum_vrijeme=sp.toString();

Upvotes: 0

Views: 752

Answers (1)

Martijn Courteaux
Martijn Courteaux

Reputation: 68857

You can get the java.util.Date object, which is one step closer to your MySQL database, instead of a String.

Date date = (Date) spinner1.getValue();

Upvotes: 1

Related Questions