Wednesday, March 18, 2009

Today the Date is Mar 18th, 2009

Hi All,

Just thought of posting my second post on Java before going to bed.

As you see from the title, you might have guessed that I would try to write a simple program that would print the Date. You are absolutely out of sync with my mind.

Ok coming to the point, if we write a program to do my fickle (I said you were out of sync) mind's problem, i.e to print today's date.....

The code a normal newbie of Java would do is:

import java.util.Calendar;
public class DatePrint {

public static void main(String[] args) {

Calendar cal = Calendar.getInstance();

System.out.println(cal.YEAR);
System.out.println(cal.MONTH);
System.out.println(cal.DAY_OF_MONTH);
}

}


If you have enough time, do run this program.

Guessing you did that, don't hit me if you did not get 2009, 3, 18.

Alright let me tell you that, the above code seems to be extremely beautiful and if you think that it yields amazing results, I am sorry.

Now just try to add the following 3 lines after the last syso (aka, System.out.Println):

System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DAY_OF_MONTH));


Note that the above 2nd syso would still print the value as 2, since March has the Integer value of 2 in Calendar class.

Hope now after this, you got the catch of mistake a newbie would do.

Now cheer up, come out of the world of newbie/fresher/inexperienced and et al.

Please do mail me if you in your Java newbie life ever did this mistake. It would be fun to post it.

Take care, Good night.

No comments:

Post a Comment