Calendar.Month Gives Wrong Output

Calendar.MONTH gives wrong value

Keep in mind that months values start from 0, so October is actually month number 9.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#MONTH

Java Calendar giving wrong value after parsing month string

You should instantiate your Calendar with appropriate locales:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"),Locale.US);

Calendar returns wrong month and year Java

Those are all static fields from the Calendar class. Java allows static fields (and methods) to be accessed on expressions that resolve to an instance's reference value the same way it does on type names

now.ERA
// is equivalent to
Calendar.ERA

The Calendar class provides a get(int) method for getting the value of a date field.

now.get(Calendar.MONTH);

Calendar.MONTH setting to wrong month

Because today's date is the 31st of August and June only has 30 days, the month is automatically incremented to the following month giving July.

To solve you can set the date before setting the month

c.set(Calendar.DATE, 30);
c.set(Calendar.MONTH, Calendar.JUNE);

Also I suggest using Calendar constants for clarity



Related Topics



Leave a reply



Submit