Java Geburtsdatum Textfeld

TheSkyRider

Mitglied
Hallo liebe Community,

ich entwickle zurzeit eine Anwendung in der Java NetBeans IDE 8.2.

In einem anderen Programm bin ich auf etwas gestoßen, was ich sehr gerne auch bei mir implementieren
würde.
Es handelt sich um ein Textfeld (für die Eingabe eines Geburtsdatums), das die Eingaben von Zahlenpaaren mit einem Punkt separiert, sodass das
Alter automatisch errechnet wird sobald man das Geburtsdatum vollständig eingetragen hat.

Ich habe auch ein Video hochgeladen, in dem ich das ganze demonstriere, weil das ansonsten etwas schwer zu erklären wäre.

Ich habe auch schon mit LocalDate und Period gearbeitet, dort brauche ich aber 3 verschiedene Textfelder
für 3 verschiedene integer: day, month und year.
Meine Ziel wäre es, wie in dem Video, alles in einem TextFeld einzutragen, sodass Tag, Monat und Jahr automatisch separiert werden
und auf die 3 integer day, month und year "verteilt" werden.

Gibt es eine Möglichkeit dies unter Java (NetBeans 8.2 IDE) zu erreichen ?

Ich danke im Voraus
 
Beste Antwort
Ach ja - da ich aktuell weiter mit ChatGPT spiele, möchte ich auch die Konversation zu diesem Thema nicht vorenthalten:

ChatGPT hat zuerst https://github.com/JDatePicker/JDatePicker vorgeschlagen - mit Beispielcode nachdem ich gefragt habe:
Is there a free java swing component to enter a date in a specified format

Yes, there are several free Java Swing components that you can use to enter a date in a specified format. One option is the JDatePicker component, which is a free, open-source component that provides a graphical user interface for selecting a date. It allows you to specify the format of the date that is displayed and entered, and it also provides a range of customization options.

Here is an...
Erst einmal ein paar Hinweise:
  • NetBeans 8.2 ist schon etwas veraltet, ggf. auf eine aktuelle Version wechseln.
  • NetBeans ist nur die Entwicklungsumgebug. Das, was Du da nutzt ist Java und vermutlich Swing. Was Du also suchst ist ein Control, dass so eine Datumseingabe zulässt.

Hier kann man zum einen nach vorhandenen Controls suchen. Da findet sich dann z.B. JCalendar: https://toedter.com/jcalendar/ bzw. https://github.com/toedter/jcalendar (Relativ alt, aber nutzt halt swing und sollte noch funktionieren)

So Controls gibt es aber bestimmt noch deutlich mehr.

Man kann sowas aber auch selbst bauen. Das ist ja unter dem Strich auch nur ein einfaches Textfeld, in dem die Daten eingegeben werden. So ein Textfeld hat dazu einige interessante Möglichkeiten. Das hier alles zu beschreiben wird etwas viel - aber dieser SO Beitrag bietet in den Antworten recht viele Links, denen man nachgehen kann: https://stackoverflow.com/questions/12655811/how-to-make-a-jtextfield-having-a-fixed-date-format
 
Ach ja - da ich aktuell weiter mit ChatGPT spiele, möchte ich auch die Konversation zu diesem Thema nicht vorenthalten:

ChatGPT hat zuerst https://github.com/JDatePicker/JDatePicker vorgeschlagen - mit Beispielcode nachdem ich gefragt habe:
Is there a free java swing component to enter a date in a specified format

Yes, there are several free Java Swing components that you can use to enter a date in a specified format. One option is the JDatePicker component, which is a free, open-source component that provides a graphical user interface for selecting a date. It allows you to specify the format of the date that is displayed and entered, and it also provides a range of customization options.

Here is an example of how you can use JDatePicker to create a date picker in a Java Swing application:
Java:
import java.util.Properties;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;

// ...

// Create a new UtilDateModel instance
UtilDateModel model = new UtilDateModel();

// Set the format of the date that will be displayed and entered
Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");

// Create a new JDatePanelImpl instance using the UtilDateModel and the specified properties
JDatePanelImpl datePanel = new JDatePanelImpl(model, p);

// Create a new JDatePickerImpl instance using the JDatePanelImpl
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());

// Add the date picker to your application's GUI
yourApplicationGUI.add(datePicker);
You can find more information about JDatePicker and how to use it in your Java applications on the project's GitHub page: https://github.com/JDatePicker/JDatePicker

Und da es ja mehr gibt, musste ich nachhaken:
Can you tell me more free components that provide such a control

There are several other free Java Swing components that provide a date picker control, including:

Each of these components provides a graphical user interface for selecting a date and allows you to specify the format of the date that is displayed and entered. They also provide various customization options, such as the ability to set a range of selectable dates or to specify the first day of the week.

Here is an example of how you can use JCalendar to create a date picker in a Java Swing application:
Java:
import java.util.Calendar;
import java.util.Locale;
import com.toedter.calendar.JCalendar;

// ...

// Create a new JCalendar instance
JCalendar calendar = new JCalendar();

// Set the format of the date that will be displayed and entered
calendar.setDateFormatString("dd.MM.yyyy");

// Set the first day of the week (Sunday or Monday)
calendar.setFirstDayOfWeek(Calendar.SUNDAY);

// Set the locale for the calendar
calendar.setLocale(Locale.GERMAN);

// Add the calendar to your application's GUI
yourApplicationGUI.add(calendar);
You can find more information about JCalendar and how to use it in your Java applications on the project's website: https://toedter.com/jcalendar/

You can find more information about Date Picker and how to use it in your Java applications on the project's GitHub page: https://github.com/JFormDesigner/date-picker

You can find more information about JXDatePicker and how to use it in your Java applications on the project's website: https://swingx.java.net/

Wichtig (Disclaimer): Diese Antworten sind nicht mein Werk sondern kommen von der Künstlichen Intelligenz, welche OpenAI unter https://chat.openai.com/chat kostenlos bereit stellt.
Meine Fragen habe ich in rot gekennzeichnet
Antworten sind in blau gekennzeichnet.
Der Code in dieser Antwort kommt komplett von ChatGPT
 
Beste Antwort
Ich habe erstmal über JDateChooser (ähnlich wie JDatePicker) eine alternative Variante gefunden mein Vorhaben zu realisieren.

Ich poste den Code mal hier rein:
Java:
try{      
        String dateofbirth = ( (JTextField)jDateChooser1.getDateEditor().getUiComponent() ).getText();
        String dob[] = dateofbirth.split("/");
       
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
        int day = Integer.parseInt(dob[0]);
        int month = Integer.parseInt(dob[1]);
        int year = Integer.parseInt(dob[2]);
       
        LocalDate birthday = LocalDate.of(year,month,day);
        LocalDate nowDate = LocalDate.now();
        int period = Period.between(birthday, nowDate).getYears();
        jTextField18.setText(String.valueOf(period));
        }
       }catch(java.time.DateTimeException e){
            JOptionPane.showMessageDialog(null, "<html><font color='red'>Datum ist ungültig!</font></html>\"" + "\n" + "\n" + e);
       }

Wichtig zu erwähnen wäre noch, dass ich beim JDateChooser das Format so gesetzt habe: dd/MM/yyyy
Das Slash wird also in der .split() Methode als Splitauslöser(?) verwendet.
 
Hallo Robert,
auch dir danke für deine Antwort.

Ich habe weiß leider nicht, wie ich mit dem DateTimeFormatter das gewünschte Ergebnis erziele,
habe es jedoch über ein JFormattedTextField erreicht.

Der obige Code ist der selbe mit einer Änderung:
Code:
String dateofbirth = jFormattedTextField1.getText();

Der custom code in der FormatterFactory (vom jFormattedTextField1) ist der hier:
Java:
new javax.swing.JFormattedTextField.AbstractFormatterFactory(){
public javax.swing.JFormattedTextField.AbstractFormatter

getFormatter(javax.swing.JFormattedTextField tf){

try{
return new javax.swing.text.MaskFormatter("##-##-####");
}catch(java.text.ParseException pe){
    pe.printStackTrace();
}
    return null;
}

}
 
Relativ einfach, du baust dir eine DateTimeFormatter Instanz mit dem gewuenschten Format und bunkerst diese irgendwo.

Java:
DateTimeFormatter dateOfBirthDateTimeFormatter = DateTimeFormatter.of("dd/MM/yyyy");

Und diesen kannst du dann jedes mal verwenden wenn du ein Datum brauchst:

Java:
LocalDate dateOfBirth = LocalDate.parse(dateOfBirthInput, dateOfBirthDateTimeFormatter);
 

Neue Themen


Zurück
Oben