Boa tarde pessoal, tenho um componente MinhaData que é utilizado em várias telas, estou tentando fazer com que ele pegue a data do sistema e preencha o campo “Data” que está presente em todas as telas da minha aplicação, mas não estou conseguindo, alguém tem uma ideia de como posso fazer?
package Componentes;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;
public class MinhaData extends MeuJFormattedTextField {
public GregorianCalendar data = new GregorianCalendar();
public MinhaData(String dica, boolean obrigatorio) {
super(dica, obrigatorio, 6);
try {
MaskFormatter mf = new MaskFormatter("##/##/####");
mf.setValidCharacters("[telefone removido]");
mf.install(this);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Não foi possível Data");
}
}
@Override
public boolean eValido() {
try {
if (getText().trim().length() != 10) {
return false;
}
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
sf.setLenient(false);
Date data = sf.parse(getText());
return true;
} catch (Exception e) {
return false;
}
}
public boolean eVazio() {
if (getText().equals(" / / ")) {
return true;
} else {
return false;
}
}
public void setText(Date date) {
setText(new SimpleDateFormat("dd/MM/yyyy").format(data));
}
public Date getValor() {
try {
return (Date) new SimpleDateFormat("dd/MM/yyyy").parse(getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Não foi possivel formatar a data.");
return null;
}
}
}