Date wierdness

T

Tim Slattery

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??
 
T

Tim Slattery

Roedy Green said:
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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top