From: Lew on
Rhino wrote:
>> Okay, I see your point now. (Well, actually it was Eric's point and
>> you are clarifying it.) But I'm not clear on what I have to do
>> differently. How do I know the object has finished construction so
>> that I know it is safe to use it? In other words, how should I code it
>> differently.
> [ SNIP ]

Arved Sandstrom wrote:
> Section 12.5 of the Java Language Specification (JLS) discusses creation
> of new class instances, but for your purposes what it boils down to is
> that the last things to execute before you get a finished ready-to-go
> object are the statements in the body of your constructor (excepting
> this() and super() statements, which occur earlier). IOW, when you leave
> the body of the constructor is when you've got your finished object.

IOW, where your code 'main()' now has 'new TestHour()', it would have instead
something similar to

TestHour th = new TestHour();
th.showResults();

or simply

new TestHour().showResults();

--
Lew
From: Rhino on
On Mar 20, 8:54 pm, Lew <no...(a)lewscanon.com> wrote:
> Rhino wrote:
> > public void testGetCurrentHour12() {
>
> > //TODO: why does this method return 9 (PM) when it is actually 8 in
> > this time zone right now?
> >   int currentHour = dateTimeUtils.getCurrentHour12HourClock();
> >   int expectedHour = 8;
> >   assertTrue("The actual hour (12 hour clock), " + currentHour + ",
> > does not equal the expected hour, " + expectedHour,
> > currentHour==expectedHour);
> >   }
>
> Just out of curiosity, if you put, just after the declaration of
> 'expectedHour', the line
>
>    Calendar calinst = Calendar.getInstance()
>    int calendarHour = calinst.get( Calendar.HOUR );
>
> what does it give you?
>
> While we're at it, what do
>
>    calinst.getTimeZone.getDisplayName()
> and
>    calinst.getTimeZone.getOffset( calinst.getTimeInMillis() )
>
> return?
>
After changing the expected result to one hour later than it actually
is, I amended the test case as follows:

-----------------------------------
public void testGetCurrentHour12() {

//TODO: why does this method return 9 (PM) when it is actually 8 in
this time zone right now?
int currentHour = dateTimeUtils.getCurrentHour12HourClock();
int expectedHour = 12;
assertTrue("The actual hour (12 hour clock), " + currentHour + ",
does not equal the expected hour, " + expectedHour,
currentHour==expectedHour); //$NON-NLS-1$ //$NON-NLS-2$
Calendar calinst = Calendar.getInstance();
int calendarHour = calinst.get( Calendar.HOUR );
System.out.println("calendarHour: " + calendarHour);
System.out.println("calinst.getTimeZone().getDisplayName() is: " +
calinst.getTimeZone().getDisplayName());

System.out.println("calinst.getTimeZone().getOffset(calinst.getTimeInMillis())
is: " + calinst.getTimeZone().getOffset( calinst.getTimeInMillis() ));
}

-----------------------------------

The result from executing it is:

-----------------------------------
calendarHour: 0
calinst.getTimeZone().getDisplayName() is: Eastern Standard Time
calinst.getTimeZone().getOffset(calinst.getTimeInMillis()) is:
-14400000
-----------------------------------

> > Are we agreed that these test cases are okay?
>
> I wonder why you write a utility method to get the current hour when the
> standard API already provides a one-line way to do so.
>
I must have missed that way in the standard API. Can you remind what
it is? I'm not sure which class you're thinking of....

--
Rhino
From: Rhino on
On Mar 20, 8:32 pm, Martin Gregorie <mar...(a)address-in-sig.invalid>
wrote:
> On Sat, 20 Mar 2010 16:49:20 -0700, Rhino wrote:
> > I'm running XP SP2, not SP3. I asked about DST issue on one of the
> > Microsoft newsgroups and they pointed me to a web page which is supposed
> > to help me install whatever I need. I went to that page, and had to
> > identify my skill level; I chose programmer and it started blathering
> > about C# and .NET but I don't develop in those. So I re- selected the
> > skill level to home user. It asked me which program I was updating; I
> > chose Outlook 2007 but it failed to execute with a cryptic error
> > message.
>
> > Frankly, I suspect the OS very strongly here but am baffled about what I
> > need to do to fix this issue.
>
> > If you can enlighten me, I'd appreciate it.
>
> I've been purely a Linux user for the last 8 years, so the last Windows
> version I understand in any depth is Win95. I still have it installed for
> the rare occasions when I need to run a couple of programs I haven't yet
> tried to run under Wine. That box dual-boots, so I am very familiar with
> local-clock Win95 and UTC Linux fighting over the RTC. My only experience
> of a more recent Window was a three month contract writing Java with
> IntelliJ on an XP box, which I treated as a black box platform since
> testing, etc. was done under RHEL.
>
> This is why I posed the questions I'd want to look into rather than
> giving you answers.
>

Fair enough. I'll obviously need to follow up with the MIcrosoft
newsgroup once we've confirmed that it is in fact as OS issue, not a
Java one.

Maybe they can tell me why DST fixing pages don't actually work.....

Thanks!

--
Rhino
From: Rhino on
On Mar 20, 8:58 pm, Lew <no...(a)lewscanon.com> wrote:
> Rhino wrote:
> >> Okay, I see your point now. (Well, actually it was Eric's point and
> >> you are clarifying it.) But I'm not clear on what I have to do
> >> differently. How do I know the object has finished construction so
> >> that I know it is safe to use it? In other words, how should I code it
> >> differently.
> > [ SNIP ]
> Arved Sandstrom wrote:
> > Section 12.5 of the Java Language Specification (JLS) discusses creation
> > of new class instances, but for your purposes what it boils down to is
> > that the last things to execute before you get a finished ready-to-go
> > object are the statements in the body of your constructor (excepting
> > this() and super() statements, which occur earlier). IOW, when you leave
> > the body of the constructor is when you've got your finished object.
>
> IOW, where your code 'main()' now has 'new TestHour()', it would have instead
> something similar to
>
>    TestHour th = new TestHour();
>    th.showResults();
>
> or simply
>
>    new TestHour().showResults();
>

Okay, I'm starting to get this. But what work would be done in the
constructor and what are you envisioning taking place in
showResults()?

I'm trying to keep everything simple since that is what the first 'S'
in SSCCE stands for ;-) Remember, I've had to copy my real code out of
DateTimeUtils and simplify it a bit for the purposes of the example.
(The key change was that I use resource bundles to look up a localized
error message in the real code; here, I just display a paraphrase of
the error message since the error message is unrelated to the
problem.)

The real DateTimeUtils has the same methods (aside from the
localization stuff) but has private constructors and public
getInstance() methods so it's not the same as TestHour in that way.

--
Rhino
From: Lew on
Lew wrote:
>> Calendar calinst = Calendar.getInstance()
>> int calendarHour = calinst.get( Calendar.HOUR );
....
>> I wonder why you write a utility method to get the current hour when the
>> standard API already provides a one-line way to do so.

Rhino wrote:
> I must have missed that way in the standard API. Can you remind what
> it is? I'm not sure which class you're thinking of....

It's in the code samples you provided in your original question:

Rhino wrote:
>>> GregorianCalendar now = new GregorianCalendar();
>>> return now.get(Calendar.HOUR);

All I did differently was to follow Eric Sosman's advice for "the recommended
`Calendar.getInstance()'".

Note that if you don't need to re-use the calendar instance, you can get the
current hour in a single line:

int calendarHour = Calendar.getInstance().get( Calendar.HOUR );

--
Lew