Buongiorno,
il mio problema consiste nel cercare di far calcolare l'età e farla comparire in un apposita TextField costruita quando l'utente digita la Data di nascita nel pannello costruito.
Vi riporto parte del codice (non completamente corretto) sviluppato fino ad ora (mi scuso per il disordine).
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class Quest extends JPanel
implements ActionListener {
private JTextField nameField;
private JTextField cognomeField;
private JTextField dataField;
public String Data_nascita;
private JTextField eta;
private JTextField Data_attuale;
public Quest (JFrame f) throws ParseException {
nameField = new JTextField(20);
cognomeField = new JTextField(20);
dataField = new JTextField(4);
eta=new JTextField(3);
Data_attuale = new JTextField(10);
JLabel label = new JLabel("Nome");
label.setLabelFor(nameField);
JLabel label1 = new JLabel("Cognome");
label.setLabelFor(cognomeField);
JLabel label3 = new JLabel("Data di nascita");
label3.setLabelFor(dataField);
JLabel label4 = new JLabel("Eta");
label4.setLabelFor(eta);
JLabel label5 = new JLabel("Data attuale");
Data_attuale.setText("04/01/2010");
label5.setLabelFor(Data_attuale);
JPanel labels = new JPanel(new GridLayout(5, 1));
labels.add(label);
labels.add(label1);
labels.add(label3);
labels.add(label4);
labels.add(label5);
JPanel fields = new JPanel(new GridLayout(5, 1));
fields.add(nameField);
fields.add(cognomeField);
fields.add(dataField);
fields.add(eta);
fields.add(Data_attuale);
Box group = Box.createHorizontalBox();
group.add(labels);
group.add(fields);
JPanel container = new JPanel(new FlowLayout(FlowLayout.RIGHT));
container.add(group);
Data_nascita= dataField.getText();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String Dataattuale = formatter.format(Data_attuale);
String Datanascita = formatter.format(Data_nascita);
Calendar cal=Calendar.getInstance();
cal.setTime(Data_nasc);
diffDates(Data_odierna,Data_nasc);
add(container);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
}
protected void resetFocus() {
nameField.requestFocusInWindow();
cognomeField.requestFocusInWindow();
}
private static void createAndShowGUI() throws ParseException {
//Create and set up the window.
JFrame frame = new JFrame("Questionario");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
//Create and set up the content pane.
final Quest newContentPane = new Quest(frame);
newContentPane.setOpaque(false); //content panes must be opaque
frame.setContentPane(newContentPane);
//Make sure the focus goes to the right component
//whenever the frame is initially given the focus.
frame.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
newContentPane.resetFocus();
}
});
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.TRUE);
createAndShowGUI();
} catch (ParseException ex) {
Logger.getLogger(Quest.class.getName()).log(Level. SEVERE, null, ex);
}
}
});
};
public static int numDaysPreviousMonth (Calendar cal)
{
Calendar c = (Calendar)cal.clone ();
c.roll (Calendar.MONTH, -1);
return c.getActualMaximum (Calendar.DAY_OF_MONTH);
}
public DiffDate diffDates (Calendar beforeDate, Calendar afterDate)
{
int carryDay = 0;
int carryMonth = 0;
int diffday = afterDate.get(Calendar.DAY_OF_MONTH) - beforeDate.get(Calendar.DAY_OF_MONTH);
if (diffday < 0)
{
diffday += numDaysPreviousMonth (afterDate);
carryDay = 1;
}
int diffmonth = afterDate.get(Calendar.MONTH) - beforeDate.get(Calendar.MONTH) - carryDay;
if (diffmonth < 0)
{
diffmonth += 12;
carryMonth = 1;
}
int diffyear = afterDate.get(Calendar.YEAR) - beforeDate.get(Calendar.YEAR) - carryMonth;
return new DiffDate (diffyear, diffmonth, diffday);
}
}
In particolare il problema è rappresentato dal fatto che il richiedere la data odierna mi causa delle eccezioni che non riesco a capire.
Ho già provato a leggere il tutorial java su Calendar e Gregorian Calendar però non sono riuscito ad andare oltre a quello che ho fatto.
Il tutto è finalizzato a creare una sorta di questionario con Nome,Cognome, Data di Nascita e altri dati in cui però voglio che l'età sia calcolata e non scritta da chi inserisce i dati.
Ringrazio anticipatamente chi avrà voglia di rispondermi.
Saluti

Rispondi quotando