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


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

Date/Calendar confusion

Received by 10.224.182.74 with SMTP id cb10mr1037006qab.0.1346918261734; Thu, 06 Sep 2012 00:57:41 -0700 (PDT)
Received by 10.52.33.34 with SMTP id o2mr84486vdi.12.1346918261686; Thu, 06 Sep 2012 00:57:41 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!b19no87373qas.0!news-out.google.com!da15ni51772qab.0!nntp.google.com!b19no87363qas.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.programmer
Date Thu, 6 Sep 2012 00:57:41 -0700 (PDT)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=62.156.183.195; posting-account=6nLwcwoAAACyuDWy5iNg9hYCXPlbqduH
NNTP-Posting-Host 62.156.183.195
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <353c7a6c-dfec-4be4-b683-3866b159aa77@googlegroups.com> (permalink)
Subject Date/Calendar confusion
From Ulrich Scholz <d7@thispla.net>
Injection-Date Thu, 06 Sep 2012 07:57:41 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.java.programmer:18567

Show key headers only | View raw


Dear all,

have a look at the function below (Java 5).  The first result is 0 as expected. But why is the second one different?

Thanks, Ulrich
 

private static void testDate() throws ParseException
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        TimeZone timeZone = TimeZone.getTimeZone("GMT");
        timeZone.setRawOffset(0); // get GMT time zone for sure
        dateFormat.setTimeZone(timeZone);

        Calendar calendar1 = Calendar.getInstance(timeZone, Locale.US);
        Date date1 = DATE_FORMAT.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 = DATE_FORMAT.parse("0000-00-00T00:00:00.000");
        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);
        calendar2.set(Calendar.DAY_OF_MONTH, calendar2.get(Calendar.DAY_OF_MONTH) + 1);

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

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


Thread

Date/Calendar confusion Ulrich Scholz <d7@thispla.net> - 2012-09-06 00:57 -0700

csiph-web