Date wierdness

Discussion in 'Java' started by Tim Slattery, Oct 24, 2005.

  1. Tim Slattery

    Tim Slattery Guest

    Can anybody explain this behavior?? This code:

    GregorianCalendar gc = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");

    System.out.println("Current date: " +
    gc.get(GregorianCalendar.MONTH) + "/" +
    gc.get(GregorianCalendar.DAY_OF_MONTH)
    + "/" + gc.get(GregorianCalendar.YEAR));
    System.out.println(sdf.format(gc.getTime()));

    gc.add(GregorianCalendar.MONTH, 6);

    System.out.println("Six months future date: " +
    gc.get(GregorianCalendar.MONTH) + "/" +
    gc.get(GregorianCalendar.DAY_OF_MONTH)
    + "/" + gc.get(GregorianCalendar.YEAR));
    System.out.println(sdf.format(gc.getTime()));


    results in this output:

    Current date: 9/24/2005
    October 2005
    Six months future date: 3/24/2006
    April 2006

    Why does SimpleDateFormat seem to bump the month up by one??

    --
    Tim Slattery
     
    Tim Slattery, Oct 24, 2005
    #1
    1. Advertising

  2. Tim Slattery

    Tim Slattery Guest

    Roedy Green <> wrote:

    >there are plenty more gotchas awaiting you. See
    >http://mindprod.com/jgloss/gotchas.html#DATE


    Thanks Roedy, that makes interesting reading.

    One nitpick: From time to time the page says something like: "If you
    live is North America this [offset from UTC] will be a negative
    number. " IMHO, that should say something like "If you live in the
    Americas" or "if you live in the Western hemisphere".

    --
    Tim Slattery
     
    Tim Slattery, Oct 25, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Olsen

    Datagrid Cell Spacing Wierdness

    Terry Olsen, Mar 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    575
    Terry Olsen
    Mar 29, 2005
  2. danthman

    Wierdness with "Imports" statement

    danthman, Dec 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,197
    danthman
    Dec 16, 2005
  3. Larry
    Replies:
    0
    Views:
    406
    Larry
    Feb 17, 2006
  4. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,333
    Michael Borgwardt
    May 30, 2004
  5. Tim Slattery

    Re: Date wierdness

    Tim Slattery, Oct 24, 2005, in forum: Java
    Replies:
    2
    Views:
    419
    Roedy Green
    Oct 25, 2005
Loading...

Share This Page