Java calendar seems to be WTFing me



  • ok so check this code snippet (the Calendar object in question is java.util.Calendar):

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, 0);
    System.out.println("WTF= " + cal.get(Calendar.MONTH));

    Calendar cal = Calendar.getInstance();

    cal.set(Calendar.MONTH, 1);

    System.out.println("WTF= " + cal.get(Calendar.MONTH)); 

    Calendar cal = Calendar.getInstance();

    cal.set(Calendar.MONTH, 2);

    System.out.println("WTF= " + cal.get(Calendar.MONTH));

     

    Program Output:

    WTF= 0
    WTF= 2
    WTF= 2

    ... i'm telling the calendar to set itself to 0 (january) then verify that it is on january, then february and verify etc.

    however when i set the calendar to 1 (february) the get method returns 2 (march), so this calendar can never be set to february, or at least it can never show me that it has been set to february.

    I really hope i'm making the mistake here, but i've tested it a lot and i'm pretty sure that it is a WTF on the part of Sun's programmers.

    So my question is this, if the error is mine, how do i fix it? or if you also say WTF, how do i submit this as a  dailyWTF?
     



  • Day of the month.



  • @Angstrom said:

    Day of the month.

    Today it doesn't work. Tomorrow it might work. Windo^h^h^h^h^hJava is like that. ;-)
     



  • @ammoQ said:

    @Angstrom said:

    Day of the month.

    Today it doesn't work. Tomorrow it might work. Windo^h^h^h^h^hJava is like that. ;-)
     

    It's doing what it's documented as doing.  Looks fine to me.  If you want to create a calendar for a specific day, use the Y/M/D constructor on GregorianCalendar instead of Calendar.getInstance ().



  • Dammit, I need my sense of humour checked. :)



  • actually it's not doing at all what it says it will. you are supposed to be able to set any one of the values specifically be it day date month year whatever. calendar.getinstance returns a calendar initialized to the current date and time. what i want to do is increment or decrement the month. so when i set Calendar.MONTH to a number, it should work. and in fact it does work for every case except february. so, nice try to make me look foolish, but you failed.



  • Ok, don't listen to me.  And definitely don't read the sections on Calendar#get(int) or the section on field manipulation in the docs.  After all, I'm only trying to make you look foolish, not solve your problem.



  • Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, 1);
    System.out.println("WTF= " + cal.get(Calendar.MONTH));   // WTF=2
    cal.set(Calendar.MONTH),2);  
    cal.set(Calendar.MONTH),1);   // WTF=1 WTF? The second time it works?
    // Giving up. Trying again tomorrow.
    cal = Calendar.getInstance();
    cal.set(Calendar.MONTH),1);   // WTF=1 WTF? Today it works.
    cal = Calendar.getInstance();
    cal.set(Calendar.DATE),31);   // This worked fine yesterday.
    System.out.println("DATE="+cal.get(Calendar.DATE));   // DATE=3 WTF only one digit?

    // Man this calendar sucks.  Different bugs every day.

     



  • "CaseyLOVESjava wrote the following post at 01-31-2007 11:23 PM:"

    Well, let me play GregorianCalendar for you.

    Casey: You are now today!

    Me: I am now 01-31-2007.

    Casey: Your month is now february.

    Me: I am now february the 31st.. yes. Wait a minute, who counts like that?

    Casey: What month are you in?

    Me: Well, I suppose I'm three days beyond february, since it only has 28 days. I must be in March already. Here, have a 2.

    Casey: Wtf?!

     

    Edit: The obvious wtf-solution is to setLenient(false); 

    Edit2: Actually, the problem won't exist for another month, so you can just deploy the code within the next 56 days and nobody will notice 



  • @CaseyLOVESjava said:

    actually it's not doing at all what it says it will. you are supposed to be able to set any one of the values specifically be it day date month year whatever. calendar.getinstance returns a calendar initialized to the current date and time. what i want to do is increment or decrement the month. so when i set Calendar.MONTH to a number, it should work. and in fact it does work for every case except february. so, nice try to make me look foolish, but you failed.

    Actually, I think he did quite a good job.

    If you actually read the JavaDocs for the Calendar, you'll find this, quite close to the top:

    Leniency

    Calendar has two modes for interpreting the calendar fields, lenient and non-lenient. When a Calendar is in lenient mode, it accepts a wider range of calendar field values than it produces. When a Calendar recomputes calendar field values for return by get(), all of the calendar fields are normalized. For example, a lenient GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1.

     



  • import java.util.Calendar;
    public class test3 {
      public static void main(String [] args) {
        Calendar cal = Calendar.getInstance();
        // OK, let's set it to January 13, 2007.
        cal.set(2007, 0000, 0013);
        System.out.println(cal.get(Calendar.DATE));
      }
    }

    Result:  11

    This calendar is so full of bugs! 

    [I'm going to say this one really IS a WTF.  I can see how octal constants are useful in C which is designed for direct access to the PDP-11 hardware, but what use are they in Java or Python?  They should require an escape sequence like hexadecimal constants: 0xB = 0O13 = 11.  Well maybe O isn't the most readable flag, but you know what I mean.]

     



  • @newfweiler said:

    This calendar is so full of bugs! 

    [I'm going to say this one really IS a WTF.  I can see how octal constants are useful in C which is designed for direct access to the PDP-11 hardware, but what use are they in Java or Python?  They should require an escape sequence like hexadecimal constants: 0xB = 0O13 = 11.  Well maybe O isn't the most readable flag, but you know what I mean.]

    This would be a problem if programs get ported from C or C++ to Java and 0013 means something different in Java than in C resp. C++. It's a stupd idea, but it's C legacy, too. 



  • @newfweiler said:

    This calendar is so full of bugs! 

    [I'm going to say this one really IS a WTF.  I can see how octal constants are useful in C which is designed for direct access to the PDP-11 hardware, but what use are they in Java or Python?  They should require an escape sequence like hexadecimal constants: 0xB = 0O13 = 11.  Well maybe O isn't the most readable flag, but you know what I mean.]

     It's a WTF, but it's not a calendar WTF, it's a Java WTF.



  • @Whiskey Tango Foxtrot? Over. said:

    It's a WTF, but it's not a calendar WTF, it's a Java WTF.

     Or it's java.util.Calendar.WTF("?");


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.