realbasic-nug
[Top] [All Lists]

Re: Java date to RB date

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Java date to RB date
From: William Squires <wsquires at satx dot rr dot com>
Date: Sun, 31 Oct 2004 21:13:53 -0600
Cc:
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <645DE11E-2B9F-11D9-B648-00039346A7A8 at mac dot com>

On Oct 31, 2004, at 6:45 PM, Steve Roy wrote:

Hm, here's a fun one. I'm parsing a text file created by another program written in Java. The date is written as the number of milliseconds since January 1, 1970, 0:00:00, as the java.util.Date class defines it. When I convert this into a REALbasic date, the result doesn't match. Here's what I do:

dim millis as String = ... // Parsed text for the Java date
dte = new Date()
dte.Day = 1
dte.Month = 1
dte.Year = 1970
dte.Hour = 0
dte.Minute = 0
dte.Second = 0
dte.TotalSeconds = dte.TotalSeconds + Val(millis) / 1000

Try "/ 1000.0" to force a floating-point division here. Val(millis) and 1000 are probably both integers, and your division then is an integer division. You could be losing stuff to rounding error. Although I consider the following an evil hack, you could move the decimal point in the string itself...

If (Len(millis) > 4) Then
  // 'millis' contains at least 5 significant "digits"
  millis = Left(millis, Len(millis)-4) + "." + Right(millis, 4)
Else
  // 'millis' contains 4 or fewer significant "digits".
  // Force it to have exactly 4.
  While (Len(millis) < 4)
    millis = "0" + millis
  Wend

  // then construct the fractional part.
  millis = "0." + millis
End If
dte.TotalSeconds = dte.TotalSeconds + Val(millis) // Look, Ma. No division!

... then when you take the Val() of it, you automatically get a floating-point number, and you can dispose of the now extraneous division... :)

When displayed in my RB app, this thusly built date shows up as
        10/25/04 12:49 AM
whereas it shows up as
        10/24/04 8:49:26 PM
in the original Java app, which means I'm 17 hours off.

I'm trying to see what could account for this difference. Do you see something I missed? I thought that by creating a new date object and making it correspond to the reference Java date (1/1/1970 0:00:00), that everything would be peachy but it's not. Is there any other way I could do this?

Steve

--
Steve Roy <sroy at mac dot com>
Personal homepage: <http://homepage.mac.com/sroy>
Projects homepage: <http://www.roydesign.net>

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>


William H Squires Jr
wsquires at satx dot rr dot com dot nospam <- remove the .nospam

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>