Javas tricky calendar and date APIs
A silly mistake can lead to a big bug, that is one common thing which we all from the
programing world know.
Look at the below program
Calendar cal = [Link]();
[Link](Calendar.HOUR_OF_DAY, 11);
[Link]([Link],51);
Date fromDate = dateFromString("2016-03-26 15:04:04", "yyyy-MM-dd HH:mm:ss");
if ([Link](fromDate))
{
[Link]("2");
}else{
[Link]("3");
}
What do you guess what will be printed , I guess you are guessing 2 but no you are wrong
ok let me give you one more chance
if ([Link](fromDate))
{
[Link]("2");
}else{
[Link]("3");
}
Now , guessing 2 no this time also 3
ok , try this
if ([Link](fromDate))
{
[Link]("2");
}else{
[Link]("3");
}
but this time also it is 3.
Surprised by the results , want to know why ??
Because of the implementation of Calendar class.
the methods after , before are meant for Calendar object but using Object as an input and then
checking the the Object instance of Calender or not,
So the expression written in if will always return false.