Last active
August 29, 2015 14:17
-
-
Save apertureless/3e12b5d620195e42fcf5 to your computer and use it in GitHub Desktop.
Java DateFormatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.util.Date; | |
import javax.swing.InputVerifier; | |
import javax.swing.JComponent; | |
import javax.swing.JFormattedTextField; | |
import javax.swing.JOptionPane; | |
public class DatumVerifier extends InputVerifier { | |
@Override | |
public boolean verify(JComponent input) { | |
String eing = ((JFormattedTextField)input).getText(); | |
DateFormat df1 = DateFormat.getDateInstance(DateFormat.MEDIUM); | |
Date d1 = null; | |
df1.setLenient(false); | |
try { | |
d1 = df1.parse(eing); | |
} catch (ParseException e) { | |
return false; | |
} | |
return (true); | |
} | |
@Override | |
public boolean shouldYieldFocus(JComponent input) { | |
if (verify(input)) { | |
return true; | |
} else { | |
JOptionPane.showMessageDialog(null, "Kein korrektes Datumformat", "Falsches Datum", JOptionPane.WARNING_MESSAGE); | |
return (false); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public MainProject() { | |
initComponents(); | |
... | |
... | |
date = new DateVerifier(); | |
MaskFormatter mform = null; | |
try { | |
mform = new MaskFormatter("##.##.####"); | |
mform.setPlaceholderCharacter('x'); | |
DefaultFormatterFactory dff = new DefaultFormatterFactory(mform); | |
jFTF_Geburtsdatum.setFormatterFactory(dff); | |
jFTF_Eintrittsdatum.setFormatterFactory(dff); | |
} catch (ParseException e) { | |
System.out.println("Falsches Format"); | |
} | |
jFTF_Geburtsdatum.setInputVerifier(date); | |
private boolean checkBirthday() { | |
String eingabe = jFTF_Geburtsdatum.getText(); | |
Calendar cal = Calendar.getInstance(); | |
Calendar cal2 = Calendar.getInstance(); | |
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy"); | |
df.setLenient(false); | |
int alter = 0; | |
try { | |
Date birth = df.parse(eingabe); | |
cal.setTime(birth); | |
int year = cal.get(Calendar.YEAR); | |
int year2 = cal2.get(Calendar.YEAR); | |
alter = year2 - year; | |
System.out.print(alter); | |
} catch (ParseException ex) { | |
Logger.getLogger(DatumVerifier.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
if (alter < 16) { | |
JOptionPane.showMessageDialog(null, "Sie müssen 16 Jahre alt sein", "Zu jung.", JOptionPane.WARNING_MESSAGE); | |
return (false); | |
} else { | |
return (true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment