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!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Ubuntu Linux: How to Login as root User - Ubuntu
- How to initiate a photo request on iPhone from Mac Monterey - iOS
- How to switch between sftp and Terminal shell - FTP
- How to Install npm using Mac Terminal - MacOS
- Notepad++ Ghost typing Examples - NotepadPlusPlus
- How to take screenshot on Android - Android
- How to write JSON file in Python Program - Python
- How to Open VS Code on Mac - MacOS