Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Peter Gade Christensen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bumpdi bump-
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was there some daylight saving time change going on during that month change? It's using add() not roll() remember.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter and Barry,

Points taken.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic