Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18606

Re: Date/Calendar confusion

From "John B. Matthews" <nospam@nospam.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Date/Calendar confusion
Date 2012-09-08 09:12 -0400
Organization The Wasteland
Message-ID <nospam-F9122E.09121908092012@news.aioe.org> (permalink)
References <3a69eb4a-f3c0-4b56-9a67-6833ccb2a1c8@googlegroups.com> <nospam-D06BA0.20534806092012@news.aioe.org> <3931f4ca-a41a-489a-8521-545eaed21742@googlegroups.com> <nospam-64D325.21022107092012@news.aioe.org> <816d0737-3c2e-476a-9a56-41a13964fe5a@googlegroups.com>

Show all headers | View raw


In article <816d0737-3c2e-476a-9a56-41a13964fe5a@googlegroups.com>,
 Lew <lewbloch@gmail.com> wrote:

> John B. Matthews wrote:
> > Lew wrote:
> >>  John B. Matthews wrote:
> >>> Ulrich Scholz  wrote:
> > 
> > [Valuable clarifications elided.]
> > 
> >>>   calendar2.set(Calendar.YEAR, 1970);
> >>>   calendar2.set(Calendar.MONTH, Calendar.JANUARY);
> >>>   calendar2.set(Calendar.DAY_OF_MONTH, 1);
> >>>
> >>>   calendar2.clear(Calendar.HOUR);
> >> 
> >> Better: 'calendar2.set(Calendar.HOUR, 0);'
> > 
> > Can I impose on you to amplify further? Is this related to "the 
> > resolution rule for the time of day," mentioned in clear(int)?
> > 
> > <http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#time_resolution>
> > <http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clear(int)> > >
> 
> I guess, but that's not my focus. My focus is on the fact that when 
> you 'clear(int)', as opposed to 'set(int,int)', the 'Calendar' 
> instance does no reconciliation, nor can you rely on any specific 
> field value such as '0'. 'clear()' sets fields to *undefined*, not a 
> specific value. It makes no attempt to reconcile field values, e.g., 
> to set a day to a valid value based on the month value or vice versa.
> 
> So you have no promise as to what the values are after a 'clear()'. 
> What comes out might well surprise, as it did on that project some 
> years ago where I encountered this situation.

Hard fought, well remembered; thanks for elaborating.

Looking closer, I see that the resulting default value is reliable for 
a particular concrete Calendar. For example, GregorianCalendar, Default 
Fields Values:

<http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html>

A potential problem is that Calendar.getInstance(TimeZone zone, 
Locale aLocale) may return an instance of a class with different 
defaults.

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Date/Calendar confusion Ulrich Scholz <d7@thispla.net> - 2012-09-06 01:03 -0700
  Re: Date/Calendar confusion nogales <nogales.manuel@gmail.com> - 2012-09-06 02:29 -0700
  Re: Date/Calendar confusion Lew <lewbloch@gmail.com> - 2012-09-06 10:21 -0700
  Re: Date/Calendar confusion "John B. Matthews" <nospam@nospam.invalid> - 2012-09-06 20:53 -0400
    Re: Date/Calendar confusion Lew <lewbloch@gmail.com> - 2012-09-07 11:01 -0700
      Re: Date/Calendar confusion "John B. Matthews" <nospam@nospam.invalid> - 2012-09-07 21:02 -0400
        Re: Date/Calendar confusion Lew <lewbloch@gmail.com> - 2012-09-07 18:44 -0700
          Re: Date/Calendar confusion "John B. Matthews" <nospam@nospam.invalid> - 2012-09-08 09:12 -0400
            Re: Date/Calendar confusion Lew <lewbloch@gmail.com> - 2012-09-08 16:51 -0700

csiph-web