0

I'm trying to get the date and time from a JDateChooserand put it on a JTextField. The date works, but the time doesn't. It shows 00:00:00.

That's my code, what am I missing?

jDateChooser1.setDate(java.sql.Date.valueOf(java.time.LocalDate.now())); //Set a standard date to the JDateChooser

 //This part is on a jButton 
    java.util.Date date;
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    date = jDateChooser1.getDate();
    String dateTime = (String) sdf.format(jDateChooser1.getDate());

    jTextField1.setText(dateTime);
  • You have to either use JSpinner or you can use [LGoodDatePicker](https://github.com/LGoodDatePicker/LGoodDatePicker) with is a good alternative to JDateChooser – Yoshikage Kira Sep 09 '19 at 19:42
  • So just to make sure I understood... Is not possible to get the time from a JDateChooser or is possible but those are just better options? Thank's anyway, I'll take a look at JSPinner and LGoodDatePicker. – Leonardo Buzzi Sep 09 '19 at 19:53
  • Possible duplicate of [Obtaining date from JDateChooser and displaying it in JTextField](https://stackoverflow.com/questions/28377318/obtaining-date-from-jdatechooser-and-displaying-it-in-jtextfield) – alexandrum Sep 09 '19 at 19:56
  • Yes. You are correct besides `LGoodDatePicker` is overall better than `JDateChooser`. Check this out [LGoodDatePicker Demo](https://i.stack.imgur.com/QXgWE.png) – Yoshikage Kira Sep 09 '19 at 19:57
  • @alexamdrum He was asking about picking time using `JDateChooser` not the date so I don't think it is a duplicate. – Yoshikage Kira Sep 09 '19 at 19:58
  • 1
    You set java.sql.Date but get java.util.Date. These 2 classes are not the same. – nickolay.laptev Sep 09 '19 at 20:15
  • Curious, what version of JDateChooser are you using? – DevilsHnd - 退職した Sep 09 '19 at 20:19
  • @nickolay.laptev Yeah, I didn't see that. I'll check it. – Leonardo Buzzi Sep 09 '19 at 20:31
  • @DevilsHnd I'm sorry, where can I see that? Is the jCalendar 1.4, if it helps. – Leonardo Buzzi Sep 09 '19 at 20:33
  • 1
    I don't think **JDateChooser** or **JCalendar** provides Time. If you want to add time then append it to a returned **date string** from the chooser before applying it to your JTextField. [This might be worth looking at as well](http://tododifferent.blogspot.com/2013/08/how-to-use-jcalendar-date-picker-in_3497.html). I dunno...I'm old...I like JDatePicker. – DevilsHnd - 退職した Sep 10 '19 at 00:35
  • 1
    I recommend you don’t use `SimpleDateFormat` and `java.sql.Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate`, `ZonedDateTime` and `DateTimeFormatter`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). I know that `JDateChooser` needs a `java.util.Date` and gives you one back. Convert like `jDateChooser1.getDate().toInstant().atZone(ZoneId.systemDefault())` and format the resulting `ZonedDateTime`. – Ole V.V. Sep 10 '19 at 09:30

0 Answers0