Java Date Examples

Getting astrological sign according to birth date
In this section we will be getting the astrological
sign according to the inserted birth date by the user. To get the astrological
sign first we require to have a date object which will be providing the inserted
date by the user.
Suppose in our example we have initialized the date,
which is to be checked with 19 December 1986.
Date date = new Date(86,12,19);
Another date object check is being used in our program
for providing the astrological sign by comparing it with another date object.
Here is the following list of date ranges according to which we are giving
astrological sign. This list is as follows:
| Date Range |
Astrological
Sign |
| December 22 - January 19 |
Capricorn |
| January 20 - February 18 |
Aquarius |
| February 19 - March 20 |
Pisces |
| March 21 - April 19 |
Aries |
| April 20 - May 20 |
Taurus |
| May 21 - June 20 |
Gemini |
| June 21 - July 22 |
Cancer |
| July 23 - August 22 |
Leo |
| August 23 - September 22 |
Virgo |
| September 23 - October 22 |
Libra |
| October 23 -November 21 |
Scorpio |
| November 22 - December 21 |
Sagittarius |
Here is the full example code of AstrologicalSign.java
as follows:
AstrologicalSign.java
import java.util.Date;
public class AstrologicalSign
{
public static void main(String args[]){
int year;
Date date = new Date(86,12,19);
Date check;
String astrologicalSign=new String("");
year = date.getYear();
while (true) {
// December 22 - Januray 19 --> Capricorn
check = new Date(year,1,20);
if(date.before(check)) {
astrologicalSign = new String("Capricorn");
break;
}
// Januray 20 - February 18 --> Aquarius
check = new Date(year,2,19);
if(date.before(check)) {
astrologicalSign = new String("Aquarius");
break;
}
// February 19 - March 20 --> Pisces
check =new Date(year,3,21);
if(date.before(check)) {
astrologicalSign = new String("Pisces");
break;
}
// March 21 - April 19 --> Aries
check = new Date(year,4,20);
if(date.before(check)) {
astrologicalSign = new String("Aries");
break;
}
// April 20 - May 20 --> Taurus
check = new Date(year,5,21);
if(date.before(check)) {
astrologicalSign = new String("Taurus");
break;
}
// May 21 - June 20 --> Gemini
check = new Date(year,6,21);
if(date.before(check)) {
astrologicalSign = new String("Gemini");
break;
}
// June 21 - July 22 --> Cancer
check = new Date(year,7,23);
if(date.before(check)) {
astrologicalSign = new String("Cancer");
break;
}
// July 23 - August 22 --> Leo
check = new Date(year,8,23);
if(date.before(check)) {
astrologicalSign = new String("Leo");
break;
}
// August 23 - September 22 --> Virgo
check = new Date(year,9,23);
if(date.before(check)) {
astrologicalSign = new String("Virgo");
break;
}
// September 23 - October 22 --> Libra
check = new Date(year,10,23);
if(date.before(check)) {
astrologicalSign = new String("Libra");
break;
}
//October 23 -November 21 --> Scorpio
check = new Date(year,11,22);
if(date.before(check)) {
astrologicalSign = new String("Scorpio");
break;
}
// November 22 - December 21 --> Sagittarius
check = new Date(year,12,22);
if(date.before(check)) {
astrologicalSign = new String("Sagittarius");
break;
}
}
System.out.println("Your astrological sign is =>> "+astrologicalSign);
}
}
|
Output:
To get the astrological sign accordingly first provide
the birth date.
C:\DateExample>javac AstrologicalSign.java
C:\DateExample>java AstrologicalSign
Your astrological sign is =>> Capricorn |
Download Source Code

|