0

How do you subtract one date from another using a Swing TextField? Is there perhaps a DateField?

Mike Partridge
  • 5,128
  • 8
  • 35
  • 47
sandip
  • 1
  • Are you looking for formatted textfields? – http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html – miku Aug 16 '11 at 20:07
  • 3
    Subtracting dates is a logical operation, not a GUI one. What do you really want to do? – Paŭlo Ebermann Aug 16 '11 at 22:36

3 Answers3

1

Use a JFormattedTextField, call myFormattedTextField.setValue(new Date()), and set the formatter to a javax.swing.text.DateFormatter

Here's a quick sample:

JFormattedTextField formattedTextField1 = new JFormattedTextField();
formattedTextField1.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(DateFormat.getDateInstance(DateFormat.SHORT))));
formattedTextField1.setValue(new Date());
Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
1

or consider that better JComponent for Date/Time/DataTime would be JSpinner

enter image description here

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

public class TimeZoneSpinners {

    private final String[] zones = {"Asia/Tokyo", "Asia/Hong_Kong",
        "Asia/Calcutta", "Europe/Paris", "Europe/London",
        "America/New_York", "America/Los_Angeles"
    };
    private final JLabel[] labels = new JLabel[zones.length];
    private final SimpleDateFormat[] formats = new SimpleDateFormat[zones.length];
    private JSpinner spinner;
    private SpinnerDateModel model;
    private SimpleDateFormat format;
    private JPanel panel;
    private JFrame frame = new JFrame();

    public void makeUI() {
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        model = new SpinnerDateModel();
        model.setValue(date);
        spinner = new JSpinner(model);
        spinner.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                Date date = (Date) ((JSpinner) e.getSource()).getValue();
                for (int i = 0; i < labels.length; i++) {
                    labels[i].setText(formats[i].format(date));
                }
            }
        });
        format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat();
        format.setTimeZone(TimeZone.getTimeZone(zones[0]));
        format.applyPattern("yyyy-MM-dd HH:mm:ss");
        panel = new JPanel(new GridLayout(zones.length, 2, 10, 10));
        for (int i = 0; i < zones.length; i++) {
            formats[i] = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
            formats[i].setTimeZone(TimeZone.getTimeZone(zones[i]));
            JLabel label = new JLabel(zones[i]);
            labels[i] = new JLabel(formats[i].format(date));
            panel.add(label);
            panel.add(labels[i]);
        }
        frame.setLayout(new BorderLayout(10, 10));
        frame.add(spinner, BorderLayout.NORTH);
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TimeZoneSpinners().makeUI();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1 I had run this nice example earlier; but looking closer, I see that it is also a good example of how `SimpleDateFormat` and `TimeZone` relate when applied to the same `Date`. – trashgod Aug 17 '11 at 10:39
  • 1
    @trashgod thanks but original idea is by another author (@Andrew Thompson), then is simple to merging with another insipration together – mKorbel Aug 17 '11 at 10:50
1

If you don't already have the two Date instances, parse() the two strings obtained from getText(), subtract the two getTime() results, and format the difference as desired.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045