Date and Calendat.getTime() not the same exact freaking thing?

Discussion in 'Java' started by lbrtchx@gmail.com, Apr 8, 2008.

  1. Guest

    Hi,
    ~
    I am parsing some data from archived files out of Unix file system
    listings
    ~
    The thing is that I decided to store the date as a long since the
    Epoch, so that people from any locales can see it accordingly by a
    simple reformatting on the fly
    ~
    But, when I parse the date data, such as "Nov 10 2003" and build a
    Calendar Object:
    ~
    System.out.println("// __ aYr: |" + aYr + "|");
    System.out.println("// __ aMonth: |" + aMonth + "|");
    System.out.println("// __ aDayOfMonth: |" + aDayOfMonth + "|");
    // __
    try{
    int iYr = Integer.parseInt(aYr);
    int iMnth = HMKMIx.get(aMonth.toUpperCase());
    int iDay = Integer.parseInt(aDayOfMonth);
    System.out.println("// __ |" + iYr + "|" + iMnth + "|" + iDay + "|");
    // __
    Calendar Kal = Calendar.getInstance();
    Kal.set(Calendar.YEAR, iYr);
    Kal.set(Calendar.MONTH, iMnth);
    Kal.set(Calendar.DAY_OF_MONTH, iDay);
    // __ http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#time
    Date Dt = Kal.getTime();
    System.out.println("// __ Dt: |" + Dt + "|");
    lTm = Dt.getTime();
    System.out.println("// __ lTm: |" + lTm + "|");
    // __
    }catch(NumberFormatException NFX){
    ~
    using:
    ~
    HMKMIx = new HashMap<String, Integer>();
    HMKMIx.put("JAN", Calendar.JANUARY);
    HMKMIx.put("FEB", Calendar.FEBRUARY);
    HMKMIx.put("MAR", Calendar.MARCH);
    HMKMIx.put("APR", Calendar.APRIL);
    HMKMIx.put("MAY", Calendar.MAY);
    HMKMIx.put("JUN", Calendar.JUNE);
    HMKMIx.put("JUL", Calendar.JULY);
    HMKMIx.put("AUG", Calendar.AUGUST);
    HMKMIx.put("SEP", Calendar.SEPTEMBER);
    HMKMIx.put("OCT", Calendar.OCTOBER);
    HMKMIx.put("NOV", Calendar.NOVEMBER);
    HMKMIx.put("DEC", Calendar.DECEMBER);
    ~
    I am getting inconsistent results
    ~
    // __ |-rw-rw-r-- 1 gbnewby pg 113214 Nov 10 2003 10002.zip|
    1/0/0/0/10002|
    // __ aYr: |2003|
    // __ aMonth: |Nov|
    // __ aDayOfMonth: |10|
    // __ |2003|10|10|
    // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    // __ lTm: |1068457579812|
    // __ |10002.zip|1/0/0/0/10002|113214|gbnewby|pg|-rw-rw-r--|
    1068457579812|
    ~
    // __ |-rw-rw-r-- 1 gbnewby pg 145317 Nov 10 2003 10003.zip|
    1/0/0/0/10003|
    // __ aYr: |2003|
    // __ aMonth: |Nov|
    // __ aDayOfMonth: |10|
    // __ |2003|10|10|
    // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    // __ lTm: |1068457579814|
    // __ |10003.zip|1/0/0/0/10003|145317|gbnewby|pg|-rw-rw-r--|
    1068457579814|
    ~
    // __ |-rw-rw-r-- 1 gbnewby pg 120858 Nov 10 2003 10004.zip|
    1/0/0/0/10004|
    // __ aYr: |2003|
    // __ aMonth: |Nov|
    // __ aDayOfMonth: |10|
    // __ |2003|10|10|
    // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    // __ lTm: |1068457579818|
    // __ |10004.zip|1/0/0/0/10004|120858|gbnewby|pg|-rw-rw-r--|
    1068457579818|
    ~
    I am using a Date object in order to get the time in millis because
    the calendar class doesn't give ti to you directly
    ~
    Why on earth is that happening? Even if they are just a few
    milliseconds apart it should not be happening?
    ~
    Is it possibly, obviously a bug or another homely and unspecified
    semantics from java's date/calendar/time handling?
    ~
    Thanks
    lbrtchx
     
    , Apr 8, 2008
    #1
    1. Advertising

  2. Bobby Quinne Guest

    Have a look at java.text.SimpleDateFomat. You should be able to get
    the Date into the desired output format

    wrote:
    > Hi,
    > ~
    > I am parsing some data from archived files out of Unix file system
    > listings
    > ~
    > The thing is that I decided to store the date as a long since the
    > Epoch, so that people from any locales can see it accordingly by a
    > simple reformatting on the fly
    > ~
    > But, when I parse the date data, such as "Nov 10 2003" and build a
    > Calendar Object:
    > ~
    > System.out.println("// __ aYr: |" + aYr + "|");
    > System.out.println("// __ aMonth: |" + aMonth + "|");
    > System.out.println("// __ aDayOfMonth: |" + aDayOfMonth + "|");
    > // __
    > try{
    > int iYr = Integer.parseInt(aYr);
    > int iMnth = HMKMIx.get(aMonth.toUpperCase());
    > int iDay = Integer.parseInt(aDayOfMonth);
    > System.out.println("// __ |" + iYr + "|" + iMnth + "|" + iDay + "|");
    > // __
    > Calendar Kal = Calendar.getInstance();
    > Kal.set(Calendar.YEAR, iYr);
    > Kal.set(Calendar.MONTH, iMnth);
    > Kal.set(Calendar.DAY_OF_MONTH, iDay);
    > // __ http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#time
    > Date Dt = Kal.getTime();
    > System.out.println("// __ Dt: |" + Dt + "|");
    > lTm = Dt.getTime();
    > System.out.println("// __ lTm: |" + lTm + "|");
    > // __
    > }catch(NumberFormatException NFX){
    > ~
    > using:
    > ~
    > HMKMIx = new HashMap<String, Integer>();
    > HMKMIx.put("JAN", Calendar.JANUARY);
    > HMKMIx.put("FEB", Calendar.FEBRUARY);
    > HMKMIx.put("MAR", Calendar.MARCH);
    > HMKMIx.put("APR", Calendar.APRIL);
    > HMKMIx.put("MAY", Calendar.MAY);
    > HMKMIx.put("JUN", Calendar.JUNE);
    > HMKMIx.put("JUL", Calendar.JULY);
    > HMKMIx.put("AUG", Calendar.AUGUST);
    > HMKMIx.put("SEP", Calendar.SEPTEMBER);
    > HMKMIx.put("OCT", Calendar.OCTOBER);
    > HMKMIx.put("NOV", Calendar.NOVEMBER);
    > HMKMIx.put("DEC", Calendar.DECEMBER);
    > ~
    > I am getting inconsistent results
    > ~
    > // __ |-rw-rw-r-- 1 gbnewby pg 113214 Nov 10 2003 10002.zip|
    > 1/0/0/0/10002|
    > // __ aYr: |2003|
    > // __ aMonth: |Nov|
    > // __ aDayOfMonth: |10|
    > // __ |2003|10|10|
    > // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    > // __ lTm: |1068457579812|
    > // __ |10002.zip|1/0/0/0/10002|113214|gbnewby|pg|-rw-rw-r--|
    > 1068457579812|
    > ~
    > // __ |-rw-rw-r-- 1 gbnewby pg 145317 Nov 10 2003 10003.zip|
    > 1/0/0/0/10003|
    > // __ aYr: |2003|
    > // __ aMonth: |Nov|
    > // __ aDayOfMonth: |10|
    > // __ |2003|10|10|
    > // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    > // __ lTm: |1068457579814|
    > // __ |10003.zip|1/0/0/0/10003|145317|gbnewby|pg|-rw-rw-r--|
    > 1068457579814|
    > ~
    > // __ |-rw-rw-r-- 1 gbnewby pg 120858 Nov 10 2003 10004.zip|
    > 1/0/0/0/10004|
    > // __ aYr: |2003|
    > // __ aMonth: |Nov|
    > // __ aDayOfMonth: |10|
    > // __ |2003|10|10|
    > // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    > // __ lTm: |1068457579818|
    > // __ |10004.zip|1/0/0/0/10004|120858|gbnewby|pg|-rw-rw-r--|
    > 1068457579818|
    > ~
    > I am using a Date object in order to get the time in millis because
    > the calendar class doesn't give ti to you directly
    > ~
    > Why on earth is that happening? Even if they are just a few
    > milliseconds apart it should not be happening?
    > ~
    > Is it possibly, obviously a bug or another homely and unspecified
    > semantics from java's date/calendar/time handling?
    > ~
    > Thanks
    > lbrtchx
     
    Bobby Quinne, Apr 8, 2008
    #2
    1. Advertising

  3. GArlington Guest

    On Apr 8, 10:06 am, wrote:
    > Hi,
    > ~
    > I am parsing some data from archived files out of Unix file system
    > listings
    > ~
    > The thing is that I decided to store the date as a long since the
    > Epoch, so that people from any locales can see it accordingly by a
    > simple reformatting on the fly
    > ~
    > But, when I parse the date data, such as "Nov 10 2003" and build a
    > Calendar Object:
    > ~
    > System.out.println("// __ aYr: |" + aYr + "|");
    > System.out.println("// __ aMonth: |" + aMonth + "|");
    > System.out.println("// __ aDayOfMonth: |" + aDayOfMonth + "|");
    > // __
    > try{
    > int iYr = Integer.parseInt(aYr);
    > int iMnth = HMKMIx.get(aMonth.toUpperCase());
    > int iDay = Integer.parseInt(aDayOfMonth);
    > System.out.println("// __ |" + iYr + "|" + iMnth + "|" + iDay + "|");
    > // __
    > Calendar Kal = Calendar.getInstance();
    > Kal.set(Calendar.YEAR, iYr);
    > Kal.set(Calendar.MONTH, iMnth);
    > Kal.set(Calendar.DAY_OF_MONTH, iDay);
    > // __http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#time
    > Date Dt = Kal.getTime();
    > System.out.println("// __ Dt: |" + Dt + "|");
    > lTm = Dt.getTime();
    > System.out.println("// __ lTm: |" + lTm + "|");
    > // __
    > }catch(NumberFormatException NFX){
    > ~
    > using:
    > ~
    > HMKMIx = new HashMap<String, Integer>();
    > HMKMIx.put("JAN", Calendar.JANUARY);
    > HMKMIx.put("FEB", Calendar.FEBRUARY);
    > HMKMIx.put("MAR", Calendar.MARCH);
    > HMKMIx.put("APR", Calendar.APRIL);
    > HMKMIx.put("MAY", Calendar.MAY);
    > HMKMIx.put("JUN", Calendar.JUNE);
    > HMKMIx.put("JUL", Calendar.JULY);
    > HMKMIx.put("AUG", Calendar.AUGUST);
    > HMKMIx.put("SEP", Calendar.SEPTEMBER);
    > HMKMIx.put("OCT", Calendar.OCTOBER);
    > HMKMIx.put("NOV", Calendar.NOVEMBER);
    > HMKMIx.put("DEC", Calendar.DECEMBER);
    > ~
    > I am getting inconsistent results
    > ~
    > // __ |-rw-rw-r-- 1 gbnewby pg 113214 Nov 10 2003 10002.zip|
    > 1/0/0/0/10002|
    > // __ aYr: |2003|
    > // __ aMonth: |Nov|
    > // __ aDayOfMonth: |10|
    > // __ |2003|10|10|
    > // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    > // __ lTm: |1068457579812|
    > // __ |10002.zip|1/0/0/0/10002|113214|gbnewby|pg|-rw-rw-r--|
    > 1068457579812|
    > ~
    > // __ |-rw-rw-r-- 1 gbnewby pg 145317 Nov 10 2003 10003.zip|
    > 1/0/0/0/10003|
    > // __ aYr: |2003|
    > // __ aMonth: |Nov|
    > // __ aDayOfMonth: |10|
    > // __ |2003|10|10|
    > // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    > // __ lTm: |1068457579814|
    > // __ |10003.zip|1/0/0/0/10003|145317|gbnewby|pg|-rw-rw-r--|
    > 1068457579814|
    > ~
    > // __ |-rw-rw-r-- 1 gbnewby pg 120858 Nov 10 2003 10004.zip|
    > 1/0/0/0/10004|
    > // __ aYr: |2003|
    > // __ aMonth: |Nov|
    > // __ aDayOfMonth: |10|
    > // __ |2003|10|10|
    > // __ Dt: |Mon Nov 10 04:46:19 EST 2003|
    > // __ lTm: |1068457579818|
    > // __ |10004.zip|1/0/0/0/10004|120858|gbnewby|pg|-rw-rw-r--|
    > 1068457579818|
    > ~
    > I am using a Date object in order to get the time in millis because
    > the calendar class doesn't give ti to you directly
    > ~
    > Why on earth is that happening? Even if they are just a few
    > milliseconds apart it should not be happening?
    > ~
    > Is it possibly, obviously a bug or another homely and unspecified
    > semantics from java's date/calendar/time handling?
    > ~
    > Thanks
    > lbrtchx


    I am not sure what is your complaint about?
    You create new instance of Calendar object (it is inited by default
    with CURRENT date/time [incl ms]), then you set a DATE, BUT do not
    change the time, and then you complain that three separate objects are
    different???
    They were NOT created at the same moment of time -> so they are bound
    to be different...
     
    GArlington, Apr 8, 2008
    #3
  4. Alan Krueger Guest

    On Apr 8, 4:06 am, wrote:
    > I am using a Date object in order to get the time in millis because
    > the calendar class doesn't give ti to you directly
    > ~
    > Why on earth is that happening? Even if they are just a few
    > milliseconds apart it should not be happening?


    Did you read the Javadoc on Calendar.getInstance()?

    "Gets a calendar using the default time zone and locale. The Calendar
    returned is based on the current time in the default time zone with
    the default locale."

    The sample code you included only sets YEAR, MONTH, and DAY_OF_MONTH.
    You're not setting the time at all, including the milliseconds, you're
    seeing a Date object for the specified date but the *time when you
    created it*. Of course this will change for each instance.

    I'd also recommend omitting the excess tildes in your post and
    unnecessary slashes in your code, it's superfluous visual noise.
     
    Alan Krueger, Apr 8, 2008
    #4
  5. Stefan Ram Guest

    Alan Krueger <> quotes:
    >"Gets a calendar using the default time zone and locale. The Calendar
    >returned is based on the current time in the default time zone with
    >the default locale."


    Actually, »current time« might be somewhat ambigous,
    as there are several times, that might be deemed »current«:

    - The time of the writing of the documentation
    - The time of the reading of the documentation
    - The time of the compilation of the call
    - The time of the execution of the call
    - The time of a read access to the calendar object

    So the wording could be changed to »based on a time that is
    not before the time of the latest sequence point before the
    call of this operation and not later than the earliest
    sequence point after the call of this operation«.
     
    Stefan Ram, Apr 8, 2008
    #5
  6. Alan Krueger Guest

    On Apr 8, 9:41 am, -berlin.de (Stefan Ram) wrote:
    > Alan Krueger <> quotes:
    >
    > >"Gets a calendar using the default time zone and locale. The Calendar
    > >returned is based on the current time in the default time zone with
    > >the default locale."

    >
    > Actually, »current time« might be somewhat ambigous,
    > as there are several times, that might be deemed »current«:
    >
    > - The time of the writing of the documentation
    > - The time of the reading of the documentation
    > - The time of the compilation of the call
    > - The time of the execution of the call
    > - The time of a read access to the calendar object


    You must be a blast at parties.
     
    Alan Krueger, Apr 8, 2008
    #6
  7. Roedy Green Guest

    Roedy Green, Apr 8, 2008
    #7
  8. Roedy Green Guest

    On Tue, 8 Apr 2008 07:51:02 -0700 (PDT), Alan Krueger
    <> wrote, quoted or indirectly quoted someone who
    said :

    >> - The time of the writing of the documentation
    >> - The time of the reading of the documentation
    >> - The time of the compilation of the call
    >> - The time of the execution of the call
    >> - The time of a read access to the calendar object

    >
    >You must be a blast at parties.


    It think he wrote it in German, then translated. Germans have special
    genes for parsing that leave the rest of us in the dust.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Apr 8, 2008
    #8
  9. Guest

    On Apr 8, 10:28 am, Alan Krueger <> wrote:
    > On Apr 8, 4:06 am, wrote:
    >
    > > I am using aDateobject in order to get the time in millis because
    > > thecalendarclass doesn't give ti to you directly
    > > ~
    > > Why on earth is that happening? Even if they are just a few
    > > milliseconds apart it should not be happening?

    >
    > Did you read the Javadoc onCalendar.getInstance()?
    >
    > "Gets acalendarusing the default time zone and locale. TheCalendar
    > returned is based on the current time in the default time zone with
    > the default locale."
    >
    > The sample code you included only sets YEAR, MONTH, and DAY_OF_MONTH.
    > You're not setting the time at all, including the milliseconds, you're
    > seeing aDateobject for the specifieddatebut the *time when you
    > created it*. Of course this will change for each instance.


    ~
    THAT DESCRIBES EXACTLY MY MISTAKEN ASSUMPTIONS. In case someone runs
    into the same issues.
    ~
    Here is it what you should do:
    ~
    in your ctor get and Instance of a Calendar and immediately set the
    Time In Millis to 0
    ~
    Kal = Calendar.getInstance();
    Kal.setTimeInMillis(0);
    ~
    then you can use a Calendar object for parsing/verifying actual dates
    and get the Epoch time, a la
    ~
    int iYr = Integer.parseInt(aYr);
    int iMnth = HMKMIx.get(aMonth.toUpperCase());
    int iDay = Integer.parseInt(aDayOfMonth);
    // __
    Kal.set(Calendar.YEAR, iYr);
    Kal.set(Calendar.MONTH, iMnth);
    Kal.set(Calendar.DAY_OF_MONTH, iDay);
    // __ http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#time
    Date Dt = Kal.getTime();
    lLModTm = Dt.getTime();
    ~
    > I'd also recommend omitting the excess tildes in your post and
    > unnecessary slashes in your code, it's superfluous visual noise.

    ~
    ... to you. It helps me jot down my comments and track progress once
    I print my relatively large code bases
    ~
    Thanks again
    lbrtchx
     
    , Apr 8, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Webb

    Need help - client's freaking

    Larry Webb, Apr 11, 2004, in forum: HTML
    Replies:
    7
    Views:
    469
    Dave Patton
    Apr 12, 2004
  2. Larry Webb

    Need help - client's freaking

    Larry Webb, Apr 11, 2004, in forum: HTML
    Replies:
    1
    Views:
    389
    Newbie
    Apr 18, 2004
  3. Denis Remezov
    Replies:
    3
    Views:
    306
    Daniel T.
    Aug 31, 2004
  4. Yann Laviolette

    Getdate and gettime

    Yann Laviolette, Nov 2, 2003, in forum: C Programming
    Replies:
    3
    Views:
    487
    Mike Wahler
    Nov 2, 2003
  5. jny0

    GetTime() library

    jny0, Jul 23, 2010, in forum: C Programming
    Replies:
    3
    Views:
    544
    Richard Bos
    Jul 25, 2010
Loading...

Share This Page