• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calendar problem  RSS feed

 
Ranch Hand
Posts: 97
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I need to get the date of a Calendar object i tried to do the following code but got a differnet result

import java.util.Calendar;
pubic class TestCalendar {

Calendar today;

public TestCalendar() {
today = Calendar.getInstance();
}
public static void main(String args[]) {
System.out.println("Todays date is:"+today.DAY_OF_MONTH+"/"+today.MONTH+
"/"+today.YEAR);
}

I was expecting
like 24/3/2005;
but I got
5/2/1

i do not understand why I got the wrong values. can any one help on this.
-Suman
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this


In your version, you print the value of the int constants.

Stefan
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might jsut add that the Calendar months start from 0, not 1, as you might expect.

The best way to print out a date is to use SimpleDateFormat. For example:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!