public static void main(String[] args) { SwingUtilities.invokeLater(() -> { TimeZone utcZone = TimeZone.getTimeZone("UTC"); Calendar utc = Calendar.getInstance(utcZone); Date date = utc.getTime(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM); dateFormat.setTimeZone(utcZone); DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM); timeFormat.setTimeZone(utcZone); JFrame frame = new JFrame(); frame.setTitle("Date Time Picker"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); DateTimePicker dateTimePicker = new DateTimePicker(date, Locale.ENGLISH, dateFormat, timeFormat); dateTimePicker.setFormats(dateFormat); dateTimePicker.setTimeFormat(timeFormat); dateTimePicker.setDateTime(date); frame.getContentPane().add(dateTimePicker); frame.pack(); frame.setVisible(true); }); } }
public DateTimePickerCellEditor(DateFormat dateFormat, DateFormat timeFormat) { super(null); Date asDate = ProductData.UTC.create(new Date(), 0).getAsDate(); DateTimePicker dateTimePicker = new DateTimePicker(asDate, Locale.getDefault(), dateFormat, timeFormat); //---- this duplicates the code in the parent constructor ------------- dateTimePicker.getEditor().setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 1)); dateTimePicker.addActionListener(getPickerActionListener()); datePicker = dateTimePicker; //--------------------------------------------------------------------- }
public void commitEdit() throws ParseException { commitTime(); super.commitEdit(); }
private void commitTime() { Date date = getDate(); if (date != null) { Date time = (Date) timeSpinner.getValue(); Calendar timeCalendar = Calendar.getInstance(timeZone); timeCalendar.setTime(time); Calendar calendar = Calendar.getInstance(timeZone); 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, timeCalendar.get(Calendar.SECOND)); calendar.set(Calendar.MILLISECOND, timeCalendar.get(Calendar.MILLISECOND)); Date newDate = calendar.getTime(); setDate(newDate); } }
public DateTimePicker(Date d, Locale l, DateFormat dateFormat, DateFormat timeFormat) { super(d, l); if (!dateFormat.getTimeZone().equals(timeFormat.getTimeZone())) { throw new IllegalStateException(String.format("Time zone mismatch: dateFormat is [%s] but timeFormat is [%s]", dateFormat, timeFormat)); } timeZone = timeFormat.getTimeZone(); getMonthView().setSelectionModel(new SingleDaySelectionModel()); getMonthView().setTimeZone(timeZone); setLinkPanel(createTimePanel()); setFormats(dateFormat); setTimeFormat(timeFormat); setDateTime(d); }
private JPanel createTimePanel() { JPanel newPanel = new JPanel(); newPanel.setLayout(new FlowLayout()); Date date = getDate(); if (date == null) { Calendar calendar = Calendar.getInstance(timeZone); date = calendar.getTime(); } SpinnerDateModel dateModel = new SpinnerDateModel(date, null, null, Calendar.DAY_OF_MONTH); 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; }
public DateFormat getTimeFormat() { return getEditor().getTimeFormat(); }
@Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { ignoreAction = true; Date valueAsDate = getValueAsDate(value); getEditor().setDateTime(valueAsDate); ignoreAction = false; return datePicker; }