Reputation: 37
Could anyone let me know why the following is displaying '5' in the console when it is the 22nd today (UK)?!
System.out.println(Calendar.DAY_OF_MONTH);
Thanks in advance!
Upvotes: 2
Views: 3966
Reputation: 11120
Calendar.DAY_OF_MONTH
is a constant used for various operations (and happens to be its value is 5
), you should be using:
Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
Upvotes: 12
Reputation: 608
Calendar.DAY_OF_MONTH or any other Calendar constants are not automatically set to the date, so return their default values.
public final static int DAY_OF_MONTH = 5;
Upvotes: 0
Reputation: 19729
You need to construct a calendar instance, then use that static field to look up the day of the month.
Calendar cal = Calendar.getInstance();
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
Upvotes: 3
Reputation: 240996
This is static
field used inside Calendar class, you want the following
calendarInstance.get(Calendar.DAY_OF_MONTH);
See
Upvotes: 4