1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Fenetre extends JFrame implements ActionListener {
JPanel pan = new JPanel();
JButton button = new JButton("Valider");
JTextField text = new JTextField();
JLabel label = new JLabel();
public Fenetre() {
{
this.setTitle("Regex date");
this.setSize(200, 150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan.setLayout(new BorderLayout());
button.addActionListener(this);
pan.add(text,BorderLayout.NORTH);
pan.add(button,BorderLayout.CENTER);
pan.add(label,BorderLayout.SOUTH);
this.setContentPane(pan);
this.setVisible(true);
}
}
public static void main(String[] args){
Fenetre fen=new Fenetre();
}
public void actionPerformed(ActionEvent e) {
String matche = this.text.getText();
Pattern p=Pattern.compile("^(29/02/((19|20)(0[48]|[2468][048]|[13579][26])|2000))|" +
"(((0[1-9]|1\\d|2[0-8])/(0[1-9]|1[0-2])/)|((29|30)/(0[13-9]|1[0-2])/)|(31/(0[13578]|1[02]))/((19|20)\\d\\d))$");
System.out.println(matche);
Matcher m=p.matcher(matche);
if (m.matches()){
label.setText("Date correct");
}
else {
label.setText("Date incorrect");
}
}
} |