Frage

How can i automatically write current system date as "YYYY-MM-DD HH:MM:SS" to formatted text field after program is launched in JAVA?

Actually i have two formatted text field. One of must be shown current system date - 5 Min second must be shown current system date after program is launched.

When i run this program Jpanel comes. Ihave these 2 formattedtextfield in this panel.

Left one: must be show current system date - 5min (2013-12-22 21:00:00.000)

Right one: must be show current system date (2013-12-22 21:05:00.000)

How can i code it for automaticly appear this values to fields?

P.S: sorry i dont attach image because i dont have 10 rep yet :(

War es hilfreich?

Lösung

You can use a SimpleDateFormat the set the text of the textfield with the current time

    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateString = formatter.format(date);
    formatText = new JFormattedTextField(createFormatter("####-##-## ##:##:##"));
    formatText.setColumns(20);
    formatText.setText(dateString);

If you need to get the date value of the input text, just parse it.

   Date date;
   try {
       date = formatter.parse(formatText.getText());
   } catch (ParseException ex) {
       ...
   }

And to set the field editable with the desired format, you can use a MaskFormatter. See my example. It runs

import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

public class MaskFormatterTest1 extends JPanel {

    private JFormattedTextField formatText;

    public MaskFormatterTest1() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateString = formatter.format(date);
        formatText = new JFormattedTextField(createFormatter("####-##-## ##:##:##"));
        formatText.setColumns(20);
        formatText.setText(dateString);

        setLayout(new BorderLayout());
        add(new JLabel("Enter Date and Time in YYYY-MM-DD HH:MM:SS format"), BorderLayout.NORTH);
        add(formatText, BorderLayout.CENTER);
    }

    private MaskFormatter createFormatter(String s) {
        MaskFormatter formatter = null;
        try {
            formatter = new MaskFormatter(s);
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
            System.exit(-1);
        }
        return formatter;
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("MaskFormatter example");
                frame.add(new MaskFormatterTest1());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

enter image description here

Andere Tipps

For setting the current system date you need:

JFormattedTextField ftf = ...;
ftf.setValue(new Date());

For setting the right format you need a DefaultFormatterFactory and the DateFormatter:

DateFormatter df = new DateFormatter(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
DefaultFormatterFactory factory = new DefaultFormatterFactory(); // other ctors possible
factory.setDefaultFormatter(df); // here simple default solution
ftf.setFormatterFactory(factory);

Note: You should read the javadoc. There are several variations for edit and display mode. So in your scenario you need two fields (and therefore two dates):

JFormattedTextField ftfLeft = ...; // create and set the format
JFormattedTextField ftfRight = ...; // create and set the format
Date now = new Date();
Date nowMinusFiveSeconds = new Date(now.getTime() - 5 *1000);
ftfLeft.setValue(nowMinusFiveSeconds);
ftfRight.setValue(now);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top