0

I have a date and time value in String type.

String dateTime = "2012-10-27 19:00:00.000";

And I have a JSpinner named spnDateTime with Spinner Model Type as Date.

What I need to do is get the String variable's value and set to the spinner's date and time .

I'm new to JAVA and this is what I tried to do.

spnDateTime.setValue(dateTime);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Dishon Michael
  • 157
  • 1
  • 2
  • 6

1 Answers1

3

You can use a SimpleDateFrmat to parse the String to a Date value

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS").parse(daetTime);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I got this error: incompatible types required: java.sql.Date found: java.util.Date – Dishon Michael Mar 31 '13 at 19:52
  • Where? As far as I'm aware, SimpleDateFormat should be returning a java.util.Date – MadProgrammer Mar 31 '13 at 19:53
  • Ok, I found what's wrong. I have imported java.sql.Date instead of import java.util.Date Now it works fine. Thanks a lot, life saver. – Dishon Michael Mar 31 '13 at 20:01
  • I also need to know how to get the value of `String time = "05:54:00.0000000"` as the value of the JSpinner same. (never mind the date in the JSpinner). – Dishon Michael Mar 31 '13 at 20:44
  • @DishonMichael the process applies. Look up SimpleDateFormat for the required values and there meanings. Your spinner will need a formatter of some kind,Mobutu I'm pretty sure if you do a quick search you'll find some examples, I think I did one – MadProgrammer Apr 01 '13 at 07:50