codice:
import java.text.SimpleDateFormat;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.Calendar;
public class myCalendar2 extends JApplet {
private static final long serialVersionUID = 1L;
JButton[] bottone;
JPanel pannelloBottoni;
JPanel pannelloCampi;
Container pannelloApplet;
public JTextField ggStart = new JTextField(10);
public JTextField mmStart = new JTextField(10);
public JTextField yyStart = new JTextField(10);
public JTextField ggEnd = new JTextField(10);
public JTextField mmEnd = new JTextField(10);
public JTextField yyEnd = new JTextField(10);
// Labels della data iniziale
public JLabel gS = new JLabel("Giorno iniziale:");
public JLabel mS = new JLabel("Mese iniziale:");
public JLabel yS = new JLabel("Anno iniziale:");
// Labels della data finale
public JLabel gE = new JLabel("Giorno finale:");
public JLabel mE = new JLabel("Mese finale:");
public JLabel yE = new JLabel("Anno finale:");
// Label TestoOutput
public JLabel Output = new JLabel("Risultato:");
public JTextArea testoOutput = new JTextArea();
public void init() {
// Settaggio testo
testoOutput.setEditable(false);
testoOutput.setText("Inserire i dati");
// Definisco il layout dell'applet:
pannelloApplet = getContentPane();
pannelloApplet.setLayout(new BorderLayout());
// inseriremo i 6 bottoni non direttamente dell'applet,
// ma in un pannello che verra' inserito nel centro dell'
// applet. Costruiamo il pannello:
pannelloBottoni = new JPanel();
// dispongo i 6 bottoni in una griglia 3 x 2:
pannelloBottoni.setLayout(new GridLayout(3, 2));
// definisco il colore di fondo del pannelloBottoni:
pannelloBottoni.setBackground(Color.black);
// definisco un array di 6 bottoni.
bottone = new JButton[9];
// inizializzo i 6 bottoni e li aggiungo al pannelloBottoni.
// Etichetta dei bottoni:
String[] nomeBottone = {"Componi data", "Data corrente", "Controllo data",
"Aggiungi tempo", "Anno bisestile", "Settimana del mese corrente",
"Settimana dell'anno corrente", "Giorno dell'anno", "bott. 9"};
// Colore di sfondo dei bottoni:
Color[] coloreBottone = {Color.red, Color.white, Color.yellow,
Color.cyan, Color.green, Color.magenta,
Color.red, Color.white, Color.yellow};
for (int i = 0; i < bottone.length; i++) {
bottone[i] = new JButton(nomeBottone[i]);
bottone[i].setBackground(coloreBottone[i]);
pannelloBottoni.add(bottone[i]);}
// inserisco il pannelloBottoni a sud dell'applet:
pannelloApplet.add(pannelloBottoni, BorderLayout.SOUTH);
// Definisco il pannello e i campi d'Input che dovrà contenere:
JPanel pannelloCampi = new JPanel();
pannelloCampi.setLayout(new GridLayout(2,6,0,0));
// Inserisco in pannelloCampi la data iniziale, quella finale ed il testo di output:
// Data START
pannelloCampi.add(gS);
pannelloCampi.add(ggStart);
pannelloCampi.add(mS);
pannelloCampi.add(mmStart);
pannelloCampi.add(yS);
pannelloCampi.add(yyStart);
// Data END
pannelloCampi.add(gE);
pannelloCampi.add(ggEnd);
pannelloCampi.add(mE);
pannelloCampi.add(mmEnd);
pannelloCampi.add(yE);
pannelloCampi.add(yyEnd);
// inserisco il pannelloCampi a nord dell'applet:
pannelloApplet.add(pannelloCampi, BorderLayout.NORTH);
// Definisco il pannello e gli Item che dovrà contenere:
JPanel pannelloOut = new JPanel();
pannelloOut.setLayout(new FlowLayout());
// OutPut
pannelloOut.add(Output);
pannelloOut.add(testoOutput);
pannelloApplet.add(pannelloOut, BorderLayout.CENTER);
Receiver1 receiver1 = new Receiver1();
Receiver2 receiver2 = new Receiver2(); // ActionListener(s)
Receiver3 receiver3 = new Receiver3(); //- da implementare dopo questa classe -
Receiver4 receiver4 = new Receiver4();
Receiver5 receiver5 = new Receiver5();
Receiver6 receiver6 = new Receiver6();
Receiver7 receiver7 = new Receiver7();
Receiver8 receiver8 = new Receiver8();
// assegnazione ActionListener(s) ai corrispondenti JButton(s) assegnazione ActionListener(s) ai corrispondenti JButton(s)
bottone[0].addActionListener(receiver1);
bottone[1].addActionListener(receiver2);
bottone[2].addActionListener(receiver3);
bottone[3].addActionListener(receiver4);
bottone[4].addActionListener(receiver5);
bottone[5].addActionListener(receiver6);
bottone[6].addActionListener(receiver7);
bottone[7].addActionListener(receiver8);
}
// la classe che definisce il ricevitore di eventi usato
// dal bottone Componi data:
class Receiver1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String giorno = ggStart.getText ();
String mese = mmStart.getText ();
String anno = yyStart.getText ();
testoOutput.setText(giorno + "/" + mese + "/" + anno + "<--------->" + anno + "/" + mese + "/" + giorno);
}
}
// la classe che definisce il ricevitore di eventi usato
// dal bottone :
class Receiver2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE d MMM yyyy", Locale.ITALY);
Date now = new Date();
testoOutput.setText(sdf.format(now) + " = " + cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.YEAR));
// testoOutput.setText();
}
}
// la classe che definisce il ricevitore di eventi usato
// dal bottone Controllo data:
class Receiver3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String SGiorno = ggStart.getText ();
String SMese = mmStart.getText ();
String SAnno = yyStart.getText ();
//Dichiarazione day, month, year:
int day = 0;
int month = 0;
int year = 0;
try {
//Parsing (all'interno del blocco try):
day = Integer.parseInt(SGiorno);
month = Integer.parseInt(SMese);
year = Integer.parseInt(SAnno);
GregorianCalendar cal = new GregorianCalendar (year, month-1, day);
cal.setLenient (false);
//Le stringhe sono oggetti "immutabili". Non esiste un metodo setText()
// né esiste un qualche altro metodo che modifica lo stato dell'oggetto String.
cal.get (Calendar.DATE);
testoOutput.setText(cal.get(Calendar.DAY_OF_WEEK) + day +"/"+ month +"/"+ year + ": data corretta.");
} catch (NumberFormatException n) {
testoOutput.setText("Per eseguire il comando \"Controllo data\" \n accertarsi che i campi Giorno, Mese e Anni iniziale siano valorizzati con cifre da 0 a 9.");
} catch (IllegalArgumentException i) {
testoOutput.setText(day +"/"+ month +"/"+ year + ": data errata.");
}
}
}
// la classe che definisce il ricevitore di eventi usato
// dal bottone Aggiungi tempo:
class Receiver4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String SGiorno = ggStart.getText ();
String SMese = mmStart.getText ();
String SAnno = yyStart.getText ();
String EGiorno = ggEnd.getText ();
String EMese = mmEnd.getText ();
String EAnno = yyEnd.getText ();
try {
//Parsing:
int day = Integer.parseInt(SGiorno);
int month = Integer.parseInt(SMese);
int year = Integer.parseInt(SAnno);
//Tempo da aggiungere
int Agg = Integer.parseInt(EGiorno);
int Amm = Integer.parseInt(EMese);
int Ayy = Integer.parseInt(EAnno);
GregorianCalendar cal = new GregorianCalendar (year, month, day);
cal.setLenient (true);
//Aggiunte
cal.add(Calendar.DAY_OF_MONTH, Agg);
cal.add(Calendar.MONTH, Amm);
cal.add(Calendar.YEAR, Ayy);
testoOutput.setText(cal.get(Calendar.YEAR) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.DAY_OF_MONTH));
} catch (NumberFormatException n) {
testoOutput.setText("Per eseguire il comando \"Aggiungi tempo\" \n accertarsi che ogni campo di input sia valorizzato con almeno una cifra da 0 a 9 \n (quindi la data iniziale DEVE essere del tutto valorizzata,e \n se si vogliono aggiungere solo giorni, i campi mese ed anno devono essere a 0).");
}
}
}
// la classe che definisce il ricevitore di eventi usato
// dal bottone Controllo data:
class Receiver5 implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Riprendo la strinda di input
String SAnno = yyStart.getText ();
//Parsing
int year = Integer.parseInt(SAnno);
int r4 = 0;
int r100 = 0;
int r400 = 0;
//Prove
r4 = year % 4;
r100 = year % 100;
r400 = year % 400;
//Bisestile o normale:
if (r400 == 0){
testoOutput.setText(year + ": divisibile per 400, bisestile (366 giorni).");
}
if (r100 == 0 && r400 != 0){
testoOutput.setText(year + ": divisibile per 100 ma non per 400, non bisestile (365 giorni).");
}
if (r4 == 0 && r100 != 0){
testoOutput.setText(year + ": divisibile per 4 ma non per 100, bisestile (366 giorni).");
}
if (r4 != 0){
testoOutput.setText(year + ": non divisibile per 4, non bisestile (365 giorni).");
}
}
}
// la classe che definisce il ricevitore di eventi usato
// dal bottone :
class Receiver6 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String SGiorno = ggStart.getText ();
String SMese = mmStart.getText ();
String SAnno = yyStart.getText ();
//Parsing:
int day = Integer.parseInt(SGiorno);
int month = Integer.parseInt(SMese);
int year = Integer.parseInt(SAnno);
GregorianCalendar cal = new GregorianCalendar (year, month-1, day);
testoOutput.setText("Settimana corrente del mese: " + cal.get (Calendar.WEEK_OF_MONTH) + "°");
}
}
class Receiver7 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String SGiorno = ggStart.getText ();
String SMese = mmStart.getText ();
String SAnno = yyStart.getText ();
//Parsing:
int day = Integer.parseInt(SGiorno);
int month = Integer.parseInt(SMese);
int year = Integer.parseInt(SAnno);
GregorianCalendar cal = new GregorianCalendar (year, month-1, day);
testoOutput.setText("Settimana corrente dell'anno: " + cal.get (Calendar.WEEK_OF_YEAR) + "°");
}
}
class Receiver8 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String SGiorno = ggStart.getText ();
String SMese = mmStart.getText ();
String SAnno = yyStart.getText ();
//Parsing:
int day = Integer.parseInt(SGiorno);
int month = Integer.parseInt(SMese);
int year = Integer.parseInt(SAnno);
GregorianCalendar cal = new GregorianCalendar (year, month-1, day);
testoOutput.setText("Giorno dell'anno: " + cal.get (Calendar.DAY_OF_YEAR) + "°");
}
}
class Receiver8 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String SGiorno = ggStart.getText ();
String SMese = mmStart.getText ();
String SAnno = yyStart.getText ();
//Parsing:
int day = Integer.parseInt(SGiorno);
int month = Integer.parseInt(SMese);
int year = Integer.parseInt(SAnno);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat fds = new SimpleDateFormat("yyyy/MM/dd");
}
}
}
Ho tre campi DA CUI "tiro fuori" la stringa, ma anche dichiarandomi 2 SimpleDateFormat per effettuare le conversioni come ho fatto qui sopra, la prima volta che clicko la stringa è vuota quindi non ha niente da convertire e in quel caso vorrei mettere in output la data nel seguente formato: yyyy/MM/dd.