java时间选择,JAVA中的日期和时间选择器

I am looking for date and time picker, but found just JCalender that's for date picker and does not support the time

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

解决方案

Check this thread (uses swingx JXDatePicker component, he extends it's functionality)

Second answer has a full example

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值