The best way to store a "date of birth" in Java is to make use of the java.time.LocalDate
class from the Date and Time API introduced in Java 8 and above.
Example:
package org.code2care.examples;
import java.time.LocalDate;
public class Employee {
private String empName;
private LocalDate empDateOfBirth;
public Employee(String empName, LocalDate empDateOfBirth) {
this.empName = empName;
this.empDateOfBirth = empDateOfBirth;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public LocalDate getEmpDateOfBirth() {
return empDateOfBirth;
}
public void setEmpDateOfBirth(LocalDate empDateOfBirth) {
this.empDateOfBirth = empDateOfBirth;
}
public int calculateAge() {
LocalDate currentDate = LocalDate.now();
return empDateOfBirth.until(currentDate).getYears();
}
public static void main(String[] args) {
LocalDate dob = LocalDate.of(1992, 12, 25);
Employee employee = new Employee("Mike", dob);
System.out.println(employee.getEmpName() + " was born on " + employee.getEmpDateOfBirth());
System.out.println(employee.getEmpName() + " is " + employee.calculateAge() + " years old.");
}
}
As you can see we made use of the until() method to calculate the age based on the date of birth and the current date. Thus, storing the date of birth in this manner allows for better date manipulation and compatibility with modern Java date and time handling.
Output:Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- Fix SharePoint Error - Unable to Display Named Item - SharePoint
- How to Uninstall Microsoft Teams on Mac - Teams
- Android Developers Bluetooth Tutorial - Android
- How to Compare two Files in Bash Shell - Bash
- See a List of All Open Ports using Mac Terminal - MacOS
- How to Save Jupyter Notebook as PDF - Python
- How to lock cells in Microsoft Excel for Mac - Windows
- How to Install npm using Mac Terminal - MacOS