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


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

Re: Date/Calendar confusion

Received by 10.66.73.170 with SMTP id m10mr488615pav.29.1346952096041; Thu, 06 Sep 2012 10:21:36 -0700 (PDT)
Received by 10.68.135.103 with SMTP id pr7mr754484pbb.7.1346952096026; Thu, 06 Sep 2012 10:21:36 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!r4no9789917pbs.0!news-out.google.com!t10ni52678833pbh.0!nntp.google.com!r4no9789910pbs.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.programmer
Date Thu, 6 Sep 2012 10:21:35 -0700 (PDT)
In-Reply-To <3a69eb4a-f3c0-4b56-9a67-6833ccb2a1c8@googlegroups.com>
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>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <da8b641c-6030-4681-81be-4ddc9ef795c7@googlegroups.com> (permalink)
Subject Re: Date/Calendar confusion
From Lew <lewbloch@gmail.com>
Injection-Date Thu, 06 Sep 2012 17:21:36 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.java.programmer:18573

Show key headers only | View raw


Ulrich Scholz wrote:
> have a look at the function below (Java 5).  The first result is 0 as expected. But why is the second one different?
> 
> private static void testDate() throws ParseException
>     {
>         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

It's lenient by default.
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean)
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#isLenient()
"The default is lenient."

Check the docs when you have a question like this.

>         TimeZone timeZone = TimeZone.getTimeZone("GMT");
>         timeZone.setRawOffset(0); // get GMT time zone for sure

WTF?

>         dateFormat.setTimeZone(timeZone);
> 
>         Calendar calendar1 = Calendar.getInstance(timeZone, Locale.US);
>         Date date1 = dateFormat.parse("1970-01-01T00:00:00.000");
>         calendar1.setTime(date1);
>         System.out.println(calendar1.getTimeInMillis()); // is 0
> 
>         Calendar calendar2 = Calendar.getInstance(timeZone, Locale.US);
>         Date date2 = dateFormat.parse("0000-00-00T00:00:00.000");

What date is that, really?

>         calendar2.setTime(date2);
>         // adjust for the epoch 01.01.1970
>         calendar2.set(Calendar.YEAR, calendar2.get(Calendar.YEAR) + 1970);
>         calendar2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH) + 1);

By this time, 'get(Calendar.MONTH)' is probably not what you think.

>         calendar2.set(Calendar.DAY_OF_MONTH, calendar2.get(Calendar.DAY_OF_MONTH) + 1);

" As a result of changing a calendar field using set(), other calendar fields may also change, depending on the calendar field, the calendar field value, and the calendar system. In addition, get(f) will not necessarily return value set by the call to the set method after the calendar fields have been recomputed. The specifics are determined by the concrete calendar class."
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

>         System.out.println(calendar2.getTimeInMillis()); // should be 0 but is -124335907200000
>     }

When you set the 'Calendar' to the invalid date, it readjusted its internal values so those 
'00' values were made into valid values.

-- 
Lew

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