+ 3

Code for Birthday

Hello everyone, I just like to write code ,that will print "Happy birth day to you". Can anyone find the problem here? if i write if(today.equals(today)) then it write, what i want.But I would like to compare today with birthday. import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class test { @SuppressWarnings("unlikely-arg-type") public static void main(String[] args) throws ParseException { Date today=new Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy"); Date birthday = sdf.parse("03-10-18"); //System.out.println(sdf.format(today)); if(today.equals(birthday)) { System.out.println("Happy birthday to you!!<3"); } } }

2nd Oct 2018, 11:35 PM
Mohamed Haque
Mohamed Haque - avatar
6 odpowiedzi
+ 2
This solved the problem. Date today=new Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy"); Date birthday = sdf.parse("03-10-18"); if(sdf.format(today).equals(sdf.format(birthday))){ System.out.println("Happy birthday to you!!<3"); } Much more simple :D Hope it helps!
3rd Oct 2018, 1:40 AM
Owenizedd
Owenizedd - avatar
+ 2
Since Date is actually a timestamp, the hours/minutes/seconds will affect your result. Another way may be to implement a Comparator to only consider the year/month/day and return 0 for a match (read up on the documentation for Comparator) I think the getYear methods etc of Date are deprecated, and you should use the Calendar class. (This also may be an avenue to explore) These are just suggestions as formatting them into strings and comparing is just fine
3rd Oct 2018, 5:27 AM
Dan Walker
Dan Walker - avatar
+ 1
Owenizedd ;Dan Walker ;Ina Thank you very much!
3rd Oct 2018, 10:05 AM
Mohamed Haque
Mohamed Haque - avatar
0
for Happy Birthday in c #include <stdio. h> #include <condo. h> int main() { print ("Happy Birthday to you"); return 0; } that's it
4th Oct 2018, 1:02 PM
Harinarayan Sahu
Harinarayan Sahu - avatar
0
lol
13th Oct 2018, 4:14 AM
Slavasil
23rd Sep 2020, 1:57 AM
Pettem Srikanth
Pettem Srikanth - avatar