ThiepLV
ThiepLV

Reputation: 1279

I can't set TimeZone to Calendar

I don't understand output of following code is 12- 12-1991,

Please explain for me, Thank for your help

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+7"));
        SimpleDateFormat simpleDF = new SimpleDateFormat("dd-MM-yyyy");
        c.set(1991, 11, 12);
        Date d = c.getTime();              
        System.out.println(simpleDF.format(d));

Upvotes: 0

Views: 162

Answers (4)

Juned Ahsan
Juned Ahsan

Reputation: 68715

From javadocs:

set

public final void set(int year,
                  int month,
                  int date)

Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH. Previous values of other calendar fields are retained. If this is not desired, call clear() first. Parameters: year - the value used to set the YEAR calendar field. month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January. date - the value used to set the DAY_OF_MONTH calendar field.

Upvotes: 1

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

months is zero index based. You need to have 10 instead of 11.

Alternatively instread of using integers directly, you can write meaningfully.

 c.set(1991,Calendar.NOVEMBER, 12);

Where Calendar.NOVEMBER is a static int field which represents NOVEMBER.

Upvotes: 1

M Abbas
M Abbas

Reputation: 6479

Month value is 0-based. e.g., 0 for January.

You have to change

c.set(1991, 11, 12);

To

c.set(1991, 10, 12);

Upvotes: 1

Balint Bako
Balint Bako

Reputation: 2560

January is Month 0, so when you set 11 that is December Check java.util.Date docs

Upvotes: 1

Related Questions