Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #18594
| Received | by 10.66.72.73 with SMTP id b9mr1280800pav.9.1347040896534; Fri, 07 Sep 2012 11:01:36 -0700 (PDT) |
|---|---|
| Received | by 10.68.125.193 with SMTP id ms1mr1670041pbb.2.1347040896517; Fri, 07 Sep 2012 11:01:36 -0700 (PDT) |
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!r4no5667297pbs.0!news-out.google.com!a8ni10693675pbd.1!nntp.google.com!r4no5667296pbs.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.java.programmer |
| Date | Fri, 7 Sep 2012 11:01:36 -0700 (PDT) |
| In-Reply-To | <nospam-D06BA0.20534806092012@news.aioe.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T |
| NNTP-Posting-Host | 69.28.149.29 |
| References | <3a69eb4a-f3c0-4b56-9a67-6833ccb2a1c8@googlegroups.com> <nospam-D06BA0.20534806092012@news.aioe.org> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <3931f4ca-a41a-489a-8521-545eaed21742@googlegroups.com> (permalink) |
| Subject | Re: Date/Calendar confusion |
| From | Lew <lewbloch@gmail.com> |
| Injection-Date | Fri, 07 Sep 2012 18:01:36 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Xref | csiph.com comp.lang.java.programmer:18594 |
Show key headers only | View raw
John B. Matthews wrote:
> Ulrich Scholz wrote:
>> calendar2.set(Calendar.YEAR, calendar2.get(Calendar.YEAR) + 1970);
>> calendar2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH) + 1);
>> calendar2.set(Calendar.DAY_OF_MONTH,
>> calendar2.get(Calendar.DAY_OF_MONTH) + 1);
>
> Note that Calendar.JANUARY is not 1. Use clear() to set some or all
> fields to a known (undefined, !isSet()) state.
DANGER!
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clear()
"Sets all the calendar field values and the time value (millisecond offset from
the Epoch) of this Calendar undefined."
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clear(int)
"Sets the given calendar field value and the time value (millisecond offset from
the Epoch) of this Calendar undefined."
These set the fields to *undefined* - not zero-equivalents.
I have seen bugs in production caused by a programmer confusing 'clear()'
with 'set(field, 0)'.
> public static void main(String[] args) {
> TimeZone timeZone = TimeZone.getTimeZone("GMT");
> SimpleDateFormat f = new SimpleDateFormat(
> "yyyy-MMM-dd HH:mm:ss.SSS Z");
>
> Calendar calendar1 = Calendar.getInstance(timeZone);
>
> System.out.println(f.format(calendar1.getTime()));
>
> calendar1.clear();
Dangerous. You need to do something to set that 'Calendar' instance
to a consistent state now.
> System.out.println(calendar1.getTimeInMillis()); // 0
>
> Calendar calendar2 = Calendar.getInstance(timeZone);
>
> System.out.println(f.format(calendar2.getTime()));
>
> 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);'
> calendar2.clear(Calendar.MINUTE);
> calendar2.clear(Calendar.SECOND);
> calendar2.clear(Calendar.MILLISECOND);
>
> System.out.println(calendar2.getTimeInMillis()); // 0
> }
--
Lew
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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