Subscribe
Tutorial search

Java Date - Java tutorial


This tutorial explains the essentials of the date object in Java based on an example - learn how to determine the astrological sign based on the birth date
Category: Java tutorials - Difficulty:




Date and time, An essential factor in a program for implementing many concepts at real-time. Java provides a special class – date Class in the java.util Package (sort of catch-all for handy utilities).

Date provides methods for examining and manipulating date and time. Time in java is measured in milliseconds since January 1, 1970. Java attempts to handle time from the system with which it is interfacing. UTC is coordinated Universal Time, which seems to be the worldwide standard.

Note : In the date class, dates before January 1, 1970 are generally not usable.

List of the methods to see full scope of the Date class:

Methods

Purpose

UTC(int, int, int, int, int)

Calculates a UTC value from YMDHMS

after(Date)

check whether this date comes after the specified date

before(Date)

Checks whether this date comes before the specified date

equals(Object)

Compares this object against specified object

getDate()

Returns the day of the month

getDay()

Returns the day of the week

getHours()

Returns the hour

getMinutes()

Returns the minute

getMonth()

Returns the month

getSeconds()

Returns the seconds

getTime()

Returns the time in milliseconds since the epoch

getTimezoneOffset()

Returns the time zone offset in minutes for the current locale that is appropriate for this time

getYear()

Returns the year after 1900

hashCode()

Compares a number that is used when storing objects in hash tables.

parse(String)

given a string representing a time, parses it and returns the time value.

setDate(int)

Sets the date

setHours(int)

Sets the hour

setMinutes(int)

Sets the minute

setMonth(int)

Sets the month

setSecondsint()

Sets the seconds

setTime(int)

Sets the time

setYear(int)

Sets the year

toGMTString()

Converts a date to string object, using the internet GMT conversions.

toLocalString()

Converts a date to string object, using the locale conversions.

toString()

Converts a date to string object, using UNIX conversions.

It is important to remember that months start with 0, which is January, and end with 11, which is December. The days of the week also start with 0, which is Sunday, and go through 6, which is Saturday. Dates of the month are normal.

Note : Dates that are not within the normal range parameters are interpreted through consecutive iterations of the date range. For example, 32 January is recognized as 1 February.

Example: Get the astrological sign based on the birth date

Now let’s write a program implementing numerous methods of Date class to find out the Astrological Sign belonging to the specified birth date.

/*program*/

import java.util.*;

public class MyMain {

public static void main (string args[]) {

BDay b;

b = new BDay(args[0]);

b.printBDayInfo();

}

}

class BDay {

String sign;

Date d;

Public BDay(String date) {

Date c;

int year;

d = new Date(date);

year = d.getyear();

while (true) {

c = new Date(“20 Jan“ + year);

If(d.before(c)) {

sign = new String(“Capricorn”);

break;

}

c = new Date(“19 Feb“ + year);

If(d.before(c)) {

sign = new String(“Aquarius”);

break;

}

c = new Date(“21 Mar“ + year);

If(d.before(c)) {

sign = new String(“Pisces”);

break;

}

c = new Date(“20 Apr“ + year);

If(d.before(c)) {

sign = new String(“Aries”);

break;

}

c = new Date(“21 May“ + year);

If(d.before(c)) {

sign = new String(“Taurus”);

break;

}

c = new Date(“21 Jun“ + year);

If(d.before(c)) {

sign = new String(“Gemini”);

break;

}

c = new Date(“23 Jul“ + year);

If(d.before(c)) {

sign = new String(“Cancer”);

break;

}

c = new Date(“23 Aug“ + year);

If(d.before(c)) {

sign = new String(“Leo”);

break;

}

c = new Date(“23 Sep“ + year);

If(d.before(c)) {

sign = new String(“Virgo”);

break;

}

c = new Date(“23 Oct“ + year);

If(d.before(c)) {

sign = new String(“Libra”);

break;

}

c = new Date(“23 Nov“ + year);

If(d.before(c)) {

sign = new String(“Scorpio”);

break;

}

c = new Date(“23 Dec“ + year);

If(d.before(c)) {

sign = new String(“Sagittarius”);

break;

}

c = new Date(“31 Dec“ + year);

If(d.before(c)) {

sign = new String(“Capricorn”);

break;

}

}

}

Public void printBDayInfo() {

Date t,c;

t = new Date();

t.setHours(0);

t.setMinutes(0);

t.setSeconds(0);

c = new Date(t.getYear(), (d.getMonth() – 1), d.getDate());

System.out.println(“You were born on: “+ (d.getMonth()+1) + “/” +

d.getDate() + “/” + d.getYear());

System.out.println(“Your sign is : “ + sign);

If (c.before(t))

System.out.println(“ your birthday is already occurred this year”);

Else

If (c.after(t)) style='color:gray'>

System.out.println(“ your birthday is yet to occur this year “);

Else

System.out.println(“TODAY IS YOUR BIRTHDAY”);

}

}

Output:

You were born on: 12/13/1985

Your sign is : Sagittarius

your birthday is yet to occur this year