JAVA 中的日期和时间选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27053276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
date and time picker in JAVA
提问by abdulla-alajmi
I am looking for date and time picker, but found just JCalender that's for date picker and does not support the time
我正在寻找日期和时间选择器,但发现只是用于日期选择器的 JCalender 并且不支持时间
I also found this http://matrex.sourceforge.net/help/DateTimePickerDialog.html
我还发现了这个 http://matrex.sourceforge.net/help/DateTimePickerDialog.html
I like it because it is simple and easy to use, but I have no idea how to get it or where can I find it
我喜欢它,因为它简单易用,但我不知道如何获得它或在哪里可以找到它
采纳答案by MihaiC
Check this thread (uses swingx JXDatePicker
component, he extends it's functionality)
检查此线程(使用 swingxJXDatePicker
组件,他扩展了它的功能)
Is there any good and free Date AND Time Picker available for Java Swing?
Second answer has a full example
第二个答案有一个完整的例子
download swingx from here: http://www.java2s.com/Code/Jar/s/Downloadswingxjar.htm
从这里下载swingx:http: //www.java2s.com/Code/Jar/s/Downloadswingxjar.htm
example from the other post:
其他帖子中的示例:
import org.jdesktop.swingx.calendar.SingleDaySelectionModel;
import org.jdesktop.swingx.JXDatePicker;
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import java.awt.*;
public class DateTimePicker extends JXDatePicker {
private JSpinner timeSpinner;
private JPanel timePanel;
private DateFormat timeFormat;
public DateTimePicker() {
super();
getMonthView().setSelectionModel(new SingleDaySelectionModel());
}
public DateTimePicker( Date d ) {
this();
setDate(d);
}
public void commitEdit() throws ParseException {
commitTime();
super.commitEdit();
}
public void cancelEdit() {
super.cancelEdit();
setTimeSpinners();
}
@Override
public JPanel getLinkPanel() {
super.getLinkPanel();
if( timePanel == null ) {
timePanel = createTimePanel();
}
setTimeSpinners();
return timePanel;
}
private JPanel createTimePanel() {
JPanel newPanel = new JPanel();
newPanel.setLayout(new FlowLayout());
//newPanel.add(panelOriginal);
SpinnerDateModel dateModel = new SpinnerDateModel();
timeSpinner = new JSpinner(dateModel);
if( timeFormat == null ) timeFormat = DateFormat.getTimeInstance( DateFormat.SHORT );
updateTextFieldFormat();
newPanel.add(new JLabel( "Time:" ) );
newPanel.add(timeSpinner);
newPanel.setBackground(Color.WHITE);
return newPanel;
}
private void updateTextFieldFormat() {
if( timeSpinner == null ) return;
JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField();
DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
// Change the date format to only show the hours
formatter.setFormat( timeFormat );
}
private void commitTime() {
Date date = getDate();
if (date != null) {
Date time = (Date) timeSpinner.getValue();
GregorianCalendar timeCalendar = new GregorianCalendar();
timeCalendar.setTime( time );
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, timeCalendar.get( Calendar.HOUR_OF_DAY ) );
calendar.set(Calendar.MINUTE, timeCalendar.get( Calendar.MINUTE ) );
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date newDate = calendar.getTime();
setDate(newDate);
}
}
private void setTimeSpinners() {
Date date = getDate();
if (date != null) {
timeSpinner.setValue( date );
}
}
public DateFormat getTimeFormat() {
return timeFormat;
}
public void setTimeFormat(DateFormat timeFormat) {
this.timeFormat = timeFormat;
updateTextFieldFormat();
}
public static void main(String[] args) {
Date date = new Date();
JFrame frame = new JFrame();
frame.setTitle("Date Time Picker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DateTimePicker dateTimePicker = new DateTimePicker();
dateTimePicker.setFormats( DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.MEDIUM ) );
dateTimePicker.setTimeFormat( DateFormat.getTimeInstance( DateFormat.MEDIUM ) );
dateTimePicker.setDate(date);
frame.getContentPane().add(dateTimePicker);
frame.pack();
frame.setVisible(true);
}
}
回答by BlakeTNC
The LGoodDatePickerlibrary includes a good DateTimePickercomponent. This component allows the user to edit both the date and the time. The time can be edited with optional "spinner" style buttons, with the default "time list" button, or with both.
该LGoodDatePicker库包括了良好的DateTimePicker组件。该组件允许用户编辑日期和时间。可以使用可选的“微调器”样式按钮、默认的“时间列表”按钮或两者来编辑时间。
Fair disclosure: I'm the primary developer.
公平披露:我是主要开发人员。
The DatePicker and the TimePicker classes can also be used individually, if desired.
I've pasted a screenshot of the three components below, (and the demo application).
如果需要,也可以单独使用 DatePicker 和 TimePicker 类。
我粘贴了以下三个组件的屏幕截图(以及演示应用程序)。
The library can be installed into your Java project from the project release page.
该库可以从项目发布页面安装到您的 Java 项目中。
The project home page is on Github at:
https://github.com/LGoodDatePicker/LGoodDatePicker.
项目主页在 Github 上:
https: //github.com/LGoodDatePicker/LGoodDatePicker。