Hallo
ich habe ein kleines Verständnisproblem.
Ich möchte gerne ein Datum in ein Feld eingeben und damit rechnen.
Habe bisher dazu folgendes erreicht. (Funktioniert alles soweit nur bekomme ich es nicht hin, das mit dem einegebenen Datum gerechnet wird, ich schaffe es nur das mit dem aktuellen Datum gerechnet wird, aber nicht mit dem eingegebenen aus dem Textfeld.)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Fenster extends JFrame
{
public static void main(String[] args)
{
new Fenster();
}
public Fenster()
{
super("Laufzeit 18 Monate berechnen");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(0, 2));
JTextField textField = new JTextField("Datum eingeben");
getContentPane().add(textField);
JButton button = new JButton("Berechnen");
getContentPane().add(button);
JLabel label = new JLabel("Gültigkeit: ");
getContentPane().add(label);
button.addActionListener(new MyListener(textField, label));
setLocation(250, 190);
setSize(250, 95);
setVisible(true);
}
}
class MyListener implements ActionListener
{
private JTextField textField;
private JLabel label;
public MyListener(JTextField tf, JLabel l)
{
textField = tf;
label = l;
}
public void actionPerformed(ActionEvent ae)
{
String text = textField.getText();
String ergebnisText = "";
try
{
// create Calendar instance with actual date
Date now = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(now);
// add 18 month to calendar instance
calendar.add(Calendar.MONTH, 18);
// add -1 day of the date
calendar.add(Calendar.DATE, -1);
// get the date instance
Date future = calendar.getTime();
// print out the dates...
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
ergebnisText = "Gültigkeit: " + dateFormat.format(future);
}
catch(NumberFormatException ex)
{
ergebnisText = "kein Datum";
}
label.setText(ergebnisText);
textField.setText("Datum eingeben");
}
}
Grundlagen im einezelnen habe ich soweit verstanden, nur wie ich es verknüpfen soll nicht so recht.
Hat jemand bitte einen Tip für mich.
ich habe ein kleines Verständnisproblem.
Ich möchte gerne ein Datum in ein Feld eingeben und damit rechnen.
Habe bisher dazu folgendes erreicht. (Funktioniert alles soweit nur bekomme ich es nicht hin, das mit dem einegebenen Datum gerechnet wird, ich schaffe es nur das mit dem aktuellen Datum gerechnet wird, aber nicht mit dem eingegebenen aus dem Textfeld.)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Fenster extends JFrame
{
public static void main(String[] args)
{
new Fenster();
}
public Fenster()
{
super("Laufzeit 18 Monate berechnen");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(0, 2));
JTextField textField = new JTextField("Datum eingeben");
getContentPane().add(textField);
JButton button = new JButton("Berechnen");
getContentPane().add(button);
JLabel label = new JLabel("Gültigkeit: ");
getContentPane().add(label);
button.addActionListener(new MyListener(textField, label));
setLocation(250, 190);
setSize(250, 95);
setVisible(true);
}
}
class MyListener implements ActionListener
{
private JTextField textField;
private JLabel label;
public MyListener(JTextField tf, JLabel l)
{
textField = tf;
label = l;
}
public void actionPerformed(ActionEvent ae)
{
String text = textField.getText();
String ergebnisText = "";
try
{
// create Calendar instance with actual date
Date now = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(now);
// add 18 month to calendar instance
calendar.add(Calendar.MONTH, 18);
// add -1 day of the date
calendar.add(Calendar.DATE, -1);
// get the date instance
Date future = calendar.getTime();
// print out the dates...
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
ergebnisText = "Gültigkeit: " + dateFormat.format(future);
}
catch(NumberFormatException ex)
{
ergebnisText = "kein Datum";
}
label.setText(ergebnisText);
textField.setText("Datum eingeben");
}
}
Grundlagen im einezelnen habe ich soweit verstanden, nur wie ich es verknüpfen soll nicht so recht.
Hat jemand bitte einen Tip für mich.