This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes One More to the K&B Errata log...Ch 6 Dates related Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "One More to the K&B Errata log...Ch 6 Dates related " Watch "One More to the K&B Errata log...Ch 6 Dates related " New topic
Author

One More to the K&B Errata log...Ch 6 Dates related

Peter Gade Christensen
Greenhorn

Joined: Sep 23, 2006
Posts: 25
SCJP5 - K&B

ch 6 - Pg 462...here is the code...

import java.util.*;
class Dates2 {
public static void main(String[] args) {
Date d1 = new Date(1000000000000L);
System.out.println("1st date " + d1.toString());
Calendar c = Calendar.getInstance();
c.setTime(d1); // #1
if(c.SUNDAY == c.getFirstDayOfWeek()) // #2
System.out.println("Sunday is the first day of the week");
System.out.println("trillionth milli day of week is "
+ c.get(c.DAY_OF_WEEK)); // #3
c.add(Calendar.MONTH, 1); // #4
Date d2 = c.getTime(); // #5
System.out.println("new date " + d2.toString() );
}
}
and here is what the book says is the ouput

1st date Sat Sep 08 19:46:40 MDT 2001
Sunday is the first day of the week
trillionth milli day of week is 7
new date Mon Oct 08 20:46:40 MDT 2001 <-----should read 19:46:40
//only month has had one added to it ..
Peter Gade Christensen
Greenhorn

Joined: Sep 23, 2006
Posts: 25
bumpdi bump-
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Was there some daylight saving time change going on during that month change? It's using add() not roll() remember.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Answering my own question: No I don't think so. Look what happens if I use the current time in my locale:

1st date Sun Oct 08 09:01:59 CEST 2006
Sunday is the first day of the week
trillionth milli day of week is 1
new date Wed Nov 08 09:01:59 CET 2006


The hour does not change, but CEST changes to CET.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
But there are some issues with the code regarding using Calendar's static values. In the original code some are referenced through object references instead of being used in a static context. I think that this is slightly better:
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
Thanks Peter and Barry,

Points taken.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
 
subject: One More to the K&B Errata log...Ch 6 Dates related
 
Similar Threads
Unable to run this code in EditPlus
Changing values of contants
How the date displays?
Calendar And Date class Doubt
please help me ...