tags:

views:

26

answers:

2

Is there any difference between the following operations? (Advance current date to 160 days)

    Calendar c = Calendar.getInstance();
    c.add(Calendar.DAY_OF_WEEK, 160);
    System.out.println(c);

    Calendar c0 = Calendar.getInstance();
    c0.add(Calendar.DAY_OF_MONTH, 160);
    System.out.println(c0);

    Calendar c1 = Calendar.getInstance();
    c1.add(Calendar.DAY_OF_YEAR, 160);
    System.out.println(c1);

I don't find any difference. So, which Calendar I should use instead?

+1  A: 

If the end result is the same, go with the one that's going to be most intuitive for people reading the code, which I'd say is Calendar.DAY_OF_YEAR.

bemace
A: 

Checking the Calendar Javadoc seems to indicate that when you're doing an add(field, delta) you're really adding the specified amount of the field type. Since the 3 types are day based, the result is the same. As @bemace said, use the most intuitive constant.

Carlos Tasada