1

Is it possible to get not only Date, but Date and Time from a JXDatePicker component?

I have configured the widget to show this format:

SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
JXDatePicker startTime = new JXDatePicker(System.currentTimeMillis); // This gets today's Date, but time appears as 00:00:00
startTime.setFormats(dateFormatter);

Whenever I select a day in the calendar, time is set to 00:00:00 even if it had any other value set manually. I don't care about that, but when I get JXDatePicker's value, I receive 00:00:00 even if it has been edited. For example, if write 2012-06-22 07:23:10 into my JXDatePicker and then call dateFormatter.format(startTime.getDate()), I'm receiving the String "2012-06-22 00:00:00".

I need to get time values. Is there any easy way to do this? If it's too hard, I could change to a diferent widget you suggest.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

5 Answers5

4

the JXDatePicker is only for extracting dates. It won't return any time. That explains why you are getting 00:00:00. You will have to find another one. maybe this is the one you are looking for:

Is there any good and free Date AND Time Picker available for Java Swing?

Community
  • 1
  • 1
Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89
2

JXDatePicker maybe should be used only for date in concept but You can still collect full Java Date object from its Editor. Just use something like this:

JXDatePicker datePicker = new JXDatePicker();
datePicker.setFormats("dd-MM-yyyy HH:mm:ss");

/*
  set in any way value inside datePicker
*/

JFormattedTextField editor = datePicker.getEditor();
Date dateInDatePicker = (Date) editor.getValue();

Using it in such way You are available to collect whole Java Date object not trimmed to only date info.

Michał Kupisiński
  • 3,765
  • 1
  • 19
  • 23
1

Posting this for others who may visit this question, rather than the OP, as I'm sure you have got a solution by now.

This is a very useful solution. It's very easy to update and customise.

http://www.java.net/node/700914

MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106
0

So simple. Just paste this into your class definitions part.

private JXDatePicker Today= new JXDatePicker(new Date()); 

This shows the system date like: "09.09.2014"
If your regional setting differs, then try formatting the date like:

datePicker.setFormats("dd/MM/yyyy");

etc...

No need to put any other stuff.

StarCrafter
  • 451
  • 5
  • 12
0
JFormattedTextField editor = jXDatePicker1.getEditor();

Date dateInDatePicker = (Date) editor.getValue();

DateFormat sysDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

String date_to_store = sysDate.format(dateInDatePicker);
Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35
Jay
  • 1