Solution 1 :
tl;dr
Generate text representing the date.
LocalDate // Represent a date-only value, without time-of-day and without time zone, using `java.time.LocalDate` class.
.of( 1982 , 1 , 24 ) // Specify a date. Notice the sane numbering of both year and month, unlike the legacy classes `Date`/`Calendar`. Returns a `LocalDate` object.
.format( // Generate text to represent the value of the `LocalDate` object.
DateTimeFormatter
.ofLocalizedDate( FormatStyle.MEDIUM ) // How long or abbreviated do you want the text.
.withLocale( Locale.UK ) // Specify a `Locale` to determine the human language and cultural norms to use in localizing.
) // Returns a `String` object.
24 Jan 1982
Calculate age.
Period
.between(
LocalDate.of( 1982 , 1 , 24 ) ,
LocalDate.now( ZoneId.systemDefault() )
)
.getYears()
Birthday toast.
MonthDay
.now(
ZoneId.systemDefault() // Specify time zone. For any given moment, the date varies around the globe by time zone.
)
.equals(
MonthDay.of( 1 , 24 )
)
java.time
The modern approach uses the java.time classes.
From your DatePicker
, pull the year, month, and day, passing all three to the factory method for LocalDate
.
DatePicker dp = … ;
…
LocalDate localDate = LocalDate.of( dp.getYear() , dp.getMonth() , dp.getDayOfMonth() ) ;
You can define your own formatting pattern when generating text for presentation to the user. But generally better to let java.time automatically localize.
LocalDate localDate = LocalDate.of( 1982 , 1 , 24 ) ;
Locale locale = Locale.UK ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( locale ) ;
String output = localDate.format( f ) ;
See this code run live at IdeOne.com.
24 Jan 1982
To calculate age, look for the LocalDate
related answers on the question: How do I calculate someone’s age in Java?.
To see if today is their birthday, use MonthDay
class.
MonthDay birthday = MonthDay.from( localDate ) ;
ZoneId z = ZoneId.of( "America/Montreal" ) ; // The time zone through which we want to see today's date.
MonthDay monthDay = MonthDay.now( z ) ;
if( monthDay.equals( birthday ) ) { … Happy Birthday … }
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later – Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
Solution 2 :
Use the Date class. Never deal with dates yourself. Its complicated.
https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
and here is a simple tutorial
https://www.javatpoint.com/java-string-to-date
So then you could check if day and month are the same for todays date and then its your birthday
EDIT: As per comments earlier, seems like Date class is no longer cool. Please take a look at the Calendar class
Java: Why is the Date constructor deprecated, and what do I use instead?
Or even better as Basil mentioned check ouy java.time classes. Although i have never used them before.
Problem :
I am learning android development, and I want to show a notification for users who have birthday today based on their current location. I have a basic DatePicker that gets the date of birth from users using these codes:
String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int monthValue = datePicker.getMonth();
String month = MONTHS[monthValue];
String dateofBirth = datePicker.getDayOfMonth() + " " + month + " " + datePicker.getYear();
the user date of birth that I got is in this string format:
24 Jan 1982
Is there a way to calculate user’s birthday age using the date string format above and show a birthday notification on that day every year? For example:
"Today is your 37th birthday!"
I think this feature would be lovely but I don’t know how to calculate it and compare it with today’s date based on different location 🙁
Your help would be very much appreciated. Thank you in advance.
Comments
Comment posted by Lionlollipop
Thank you! I learnt so much from you and will try it out
Comment posted by Basil Bourque
That terrible
Comment posted by Basil Bourque
Calendar
Comment posted by Basil Bourque
Sun, Oracle, and the JCP community unanimously decided those classes
Comment posted by Joakim Danielson
First you write an answer and then you revert it and even point to another answer, so why didn’t you simply remove this answer?
Comment posted by Ole V.V.
Never deal with dates yourself. Its complicated.