user3013144
user3013144

Reputation: 61

Displaying the set date using Calendar..the output is off. I do not know why

I do not understand why the output to the date that I have set (2013, 01, 21) gets displayed as: Thu Feb 21 23:05:18 GMT2013. Instead of January, the output here is Feb??

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class date {

public static void main(String[] args) {

    Calendar myCal = Calendar.getInstance();
    myCal.set(2013,01,21);
    Date bestBeforeDate = myCal.getTime();
    System.out.println(bestBeforeDate);

Upvotes: 0

Views: 24

Answers (1)

rgettman
rgettman

Reputation: 178253

Because months are ranged 0-11 in Java, 0 being January, 1 being February, ...

Calendar javadocs for MONTH:

The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

Upvotes: 2

Related Questions