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 / First Aid / August 2004

Tip: Looking for answers? Try searching our database.

Date/Calendar confusion

Thread view: 
Kurt Harless - 30 Aug 2004 02:02 GMT
Greetings,

Given snippet...

       Date today;
       today = new Date();
       Calendar todayg = Calendar.getInstance();
       System.out.println("Date: " + today);
       System.out.println("Dateg: " + todayg.SECOND);
       System.out.println("Dateg: " + todayg);

The output contains;

Date: Sun Aug 29 17:57:26 GMT-07:00 2004
Dateg: 13
Dateg:
java.util.GregorianCalendar[time=1093827446353,areFieldsSet=true,areAllField
sSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT-07:00",offset
=-25200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],first
DayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=7,
WEEK_OF_YEAR=36,WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=242,DAY_OF_WEEK=
1,DAY_OF_WEEK_IN_MONTH=5,
AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=57,SECOND=26,MILLISECOND=353,ZONE_OFFSE
T=-25200000,DST_OFFSET=0]

Why does "System.out.println("Dateg: " + todayg.SECOND);" produce a value of
13 when "System.out.println("Dateg: " + todayg);"  has SECOND=26 ????

Dazed and Confused...
Murray - 30 Aug 2004 02:37 GMT
> Greetings,
>
[quoted text clipped - 12 lines]
> Dateg: 13
> Dateg:

java.util.GregorianCalendar[time=1093827446353,areFieldsSet=true,areAllField

sSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT-07:00",offset

=-25200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],first
> DayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=7,

WEEK_OF_YEAR=36,WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=242,DAY_OF_WEEK=
> 1,DAY_OF_WEEK_IN_MONTH=5,

AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=57,SECOND=26,MILLISECOND=353,ZONE_OFFSE
> T=-25200000,DST_OFFSET=0]
>
> Why does "System.out.println("Dateg: " + todayg.SECOND);" produce a value of
> 13 when "System.out.println("Dateg: " + todayg);"  has SECOND=26 ????
>
> Dazed and Confused...

Because Calendar.SECOND does not represent the seconds value, it is simply a
static (constant) integer representing the field number, which happens to be
the 13th field in Calendar. Your code will always print 13 regardless of the
time the calendar was instantiated. Similarly, todayg.YEAR will print 1,
todayg.MONTH will be 2 ... etc

I think what you're after is todayg.get(Calendar.SECOND)
Kurt Harless - 30 Aug 2004 03:36 GMT
Much thanks!!!

Less Dazed, still Confused

> > Greetings,
> >
[quoted text clipped - 12 lines]
> > Dateg: 13
> > Dateg:

java.util.GregorianCalendar[time=1093827446353,areFieldsSet=true,areAllField

sSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT-07:00",offset

=-25200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],first
> > DayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=7,

WEEK_OF_YEAR=36,WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=242,DAY_OF_WEEK=
> > 1,DAY_OF_WEEK_IN_MONTH=5,

AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=57,SECOND=26,MILLISECOND=353,ZONE_OFFSE
> > T=-25200000,DST_OFFSET=0]
> >
[quoted text clipped - 11 lines]
>
> I think what you're after is todayg.get(Calendar.SECOND)
Murray - 30 Aug 2004 09:01 GMT
> Much thanks!!!
>
> Less Dazed, still Confused

One down, one to go ... :-)

What are you still confused about? Or is that just your normal mental state?
:-P


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



©2008 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.