简体   繁体   中英

Getting time from JXDatePicker

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.

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?

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.

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

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM