Calendar Question

When i run this code,i expect the date object to be printed as "Sat Sep 30 22:44:00 CDT 2006"

But when i run this code when the system time is AM. i get what i expetced ie "Sat Sep 30 22:44:00 CDT 2006"

when i run this code when system time is PM i get "Sun Oct 01 10:44:00 CDT 2006"

why is this strange behaviour?

Calendar c = Calendar.getInstance();

int year = 2006;

int month = 9;

int d = 30;

int hour =22;

int minute=44;

int seonds = 0;

c.set(year,month-1,d);

c.set(Calendar.HOUR,hour );

c.set(Calendar.MINUTE,minute);

c.set(Calendar.SECOND,seonds );

System.out.println(c.getTime());

[693 byte] By [Kingsizea] at [2007-10-3 3:02:20]
# 1
If you are using a 24 hour clock you need to use Calendar.HOUR_OF_DAY.Ted.
ted_trippina at 2007-7-14 20:52:05 > top of Java-index,Java Essentials,Java Programming...
# 2
nm
Herko_ter_Horsta at 2007-7-14 20:52:05 > top of Java-index,Java Essentials,Java Programming...
# 3

First, you should not do this: c.set(year,month-1,d);

The numerical values for the month constants are meant to be opaque. You should not know or care what they are. You should be using Calendar.SEPTEMBER, for example.

Calendar is more useful for manipulating dates--adding or subtracting days, weeks, months, etc.--than for setting and displaying dates. A better approach might be to use SimpleDateFormat to parse a String, and then the same or a different SDF (depending on whether you want the output in the same format as the input) to display it. You can also adjust the TZ in the SDF if you need to.

jverda at 2007-7-14 20:52:05 > top of Java-index,Java Essentials,Java Programming...
# 4
Yes i agree with you Earlier i used the SimpleDateFormat,but due to the Performace issue[SimpleDateFormat Construction is a heavy one and there are synchronized methods in it]i switched back to this approach
Kingsizea at 2007-7-14 20:52:05 > top of Java-index,Java Essentials,Java Programming...
# 5

> Yes i agree with you

>

> Earlier i used the SimpleDateFormat,but due to the

> Performace issue[SimpleDateFormat Construction is a

> heavy one and there are synchronized methods in it]

>

> i switched back to this approach

Did you measure performance problems that could be traced directly to SDF?

As for synchronization, if you need it, then you need it, and if you don't, then it will be uncontested, and I thought that uncontested obtain/release lock was very fast.

jverda at 2007-7-14 20:52:05 > top of Java-index,Java Essentials,Java Programming...