Ash1986
Ash1986

Reputation: 21

Odd Java Calendar behavior

Does anyone understand why is the year in the output 2077 instead of 2011?

Integer yyyyMMdd = 20110830
Calendar day = Calendar.getInstance(TimeZone.DEFAULT);

Integer dd = yyyyMMdd % 100;
Integer yyyy = yyyyMMdd / 10000;

day.set(yyyy, MM-1, dd);

System.err.println(day.getTimeInMillis());

Upvotes: 2

Views: 122

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500465

Having fixed up your code so that it actually compiles, I get the expected result - so presumably it's a bug in the code you were really running but hadn't shown. Here's my code:

import java.util.*;

class Test {
    public static void main(String[] args) {
        int yyyyMMdd = 20110830;
        Calendar day = Calendar.getInstance(TimeZone.getDefault());

        int dd = yyyyMMdd % 100;
        int MM = (yyyyMMdd % 10000) / 100;
        int yyyy = yyyyMMdd / 10000;

        day.set(yyyy, MM-1, dd);

        System.err.println(day.getTime());
    }
}

Result on my machine:

Tue Aug 30 07:18:33 BST 2011

Upvotes: 5

Related Questions