// Cria mascara para o campo de texto.
MaskFormatter mf = new MaskFormatter("##/##/####");
// Cria campo de texto formatado de acordo com a mascara acima.
JFormattedTextField textField = new JFormattedTextField(mf);
// Define a data. Nao eh necessario colocar as barras, pois a mascara
// toma conta disso. O importante eh definir a data corretamente:
// 2 digitos para dia, 2 para mes, 4 para ano.
textField.setText("15/12/2011");
// commitEdit() confirma a edicao: isso eh importante. Veja a definicao
// abaixo:
// "The text property is defined by the JTextField class. This property
// always reflects what the field displays. The value property, defined
// by the JFormattedTextField class, might not reflect the latest text
// displayed in the field. While the user is typing, the text property
// changes, but the value property does not change until the changes
// are committed."
textField.commitEdit();
// Retorna o texto do campo de texto, apos a confirmacao (commit).
String text = textField.getValue().toString();
// Separa o texto em campos para pegar dia, mes e ano.
String[] split = text.split("/");
String dia = split[0];
String mes = split[1];
String ano = split[2];
System.out.println("dia = "+dia);
System.out.println("mes = "+mes);
System.out.println("ano = "+ano);
JavaDocs:
MaskFormatter
JFormattedTextField
commitEdit()
String
System
Tutoriais How to Use Formatted Text Fields