Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Calendar

Thread view: 
Vova Reznik - 18 Oct 2005 18:58 GMT
Does anyone know why

Calendar c = Calendar.getInstance();
c.set(Calendar.AM_PM, Calendar.AM);
System.out.println(c.get(Calendar.AM_PM));

will always print 1 [PM]
Roedy Green - 18 Oct 2005 19:52 GMT
>Calendar c = Calendar.getInstance();
>c.set(Calendar.AM_PM, Calendar.AM);
>System.out.println(c.get(Calendar.AM_PM));

c already contained a date and time timestamp.  It already KNEW the
time was after noon.  The AM_PM field would only be relevant if you
composed the field from bits and pieces including an hour.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

P.Hill - 19 Oct 2005 05:31 GMT
>>Calendar c = Calendar.getInstance();
>>c.set(Calendar.AM_PM, Calendar.AM);
>>System.out.println(c.get(Calendar.AM_PM));
>
> The AM_PM field would only be relevant if you
> composed the field from bits and pieces including an hour.

Curiously enough that is the feature and it IS documented:

"Inconsistent information. If fields conflict, the calendar will give
preference to fields set more recently. For example, when determining
the day, the calendar will look for one of the following combinations of
fields. The most recent combination, as determined by the most recently
set single field, will be used.

[...]

For the time of day:

 HOUR_OF_DAY
 AM_PM + HOUR

"
see
 http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html

So in your case it didn't come up with AM_PM and the HOUR _combination_
as having been set, so it used the old HOUR_OF_DAY. The solution is to
change HOUR when you change AM_PM which could include getting the
existing value, thus the following code results in
the value you expected:

        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR, c.get(Calendar.HOUR));
        c.set(Calendar.AM_PM, Calendar.AM);
        System.out.println(c.get(Calendar.AM_PM));

-Paul


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.