Click to See Complete Forum and Search --> : date problem
pouncer
May 25th, 2010, 06:14 AM
Calendar cal1 = Calendar.getInstance();
cal1.set(2010, 01, 29);
Date date = cal1.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String strDate = formatter.format(date);
System.out.println("str date.......... " + strDate);
It prints out
str date.......... 01/03/2010
why isnt it printing 29/01/2010
dlorde
May 25th, 2010, 06:53 AM
It pays to read the API docs - Java Calendar months start at 0 for January, so you are formatting the 29th of February for 2010 which isn't a leap year, so it rolls over to the start of the next month.
If you use the Calendar constants, it will work as expected:cal1.set(2010, Calendar.JANUARY, 29);
Unless in communicating with it [a computer] one says exactly what one means, trouble is bound to result...
A. Turing
ProgramThis
May 25th, 2010, 08:31 AM
Which I always used to get confused myself since everything else in the calendar class is NOT zero based. A little consistency guys?
The first month of the year is JANUARY which is 0; the last depends on the number of months in a year.
The first week of the year, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1
The first day of the month has value 1.
keang
May 25th, 2010, 03:03 PM
Which I always used to get confused myself since everything else in the calendar class is NOT zero based. A little consistency guys?The whole of the date/calendar system is a mess, hence people who need to do serious work with dates tend to use a third party implementation such as Joda Time instead.
I believe a system similar to Joda time (JSR 310) is planned for inclusion in Java release 7 and not before time if you ask me.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.