Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Convert a Java timestamp to C# DateTime object

Convert a Java timestamp to C# DateTime object

Scheduled Pinned Locked Moved C#
questioncsharpjavadatabasemysql
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cyrus_virus
    wrote on last edited by
    #1

    Hi, i have a Java application that stores a timestamp object as a bigint in a MySql database. Now, my C# application has to convert it to a DateTime object. How do i do this. I tried using the Datetime.FromFileTime(timestamp); but the output it gives is not correct. Here is the sample.

        long tm = 1217404627332;
    
        DateTime dt = DateTime.FromFileTime(tm);
        Response.Write("Time: " + dt.ToLongDateString());
    

    The output it gives is Tuesday, January 02, 1601 when it should be Wednesday, July 31, 2008 How do i do this? Any help is appreciated. Thanks in advance.

    C M C 3 Replies Last reply
    0
    • C cyrus_virus

      Hi, i have a Java application that stores a timestamp object as a bigint in a MySql database. Now, my C# application has to convert it to a DateTime object. How do i do this. I tried using the Datetime.FromFileTime(timestamp); but the output it gives is not correct. Here is the sample.

          long tm = 1217404627332;
      
          DateTime dt = DateTime.FromFileTime(tm);
          Response.Write("Time: " + dt.ToLongDateString());
      

      The output it gives is Tuesday, January 02, 1601 when it should be Wednesday, July 31, 2008 How do i do this? Any help is appreciated. Thanks in advance.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      One approach would be to search for how the number translates to a date in Java, and write code to do a conversion.

      Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

      1 Reply Last reply
      0
      • C cyrus_virus

        Hi, i have a Java application that stores a timestamp object as a bigint in a MySql database. Now, my C# application has to convert it to a DateTime object. How do i do this. I tried using the Datetime.FromFileTime(timestamp); but the output it gives is not correct. Here is the sample.

            long tm = 1217404627332;
        
            DateTime dt = DateTime.FromFileTime(tm);
            Response.Write("Time: " + dt.ToLongDateString());
        

        The output it gives is Tuesday, January 02, 1601 when it should be Wednesday, July 31, 2008 How do i do this? Any help is appreciated. Thanks in advance.

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        The Windows FILETIME datatype, and hence .NET's DateTime, counts in 100ns intervals: there are 10 million of them in a single second. The base date is 1 January 1601. Your number, big though it is, only means 121,740 seconds or a little less than 34 hours. You need to find out how large a time interval one tick represents in your source data, and you need to find out what the base date is, so that you can manipulate it correctly for .NET.

        DoEvents: Generating unexpected recursion since 1991

        1 Reply Last reply
        0
        • C cyrus_virus

          Hi, i have a Java application that stores a timestamp object as a bigint in a MySql database. Now, my C# application has to convert it to a DateTime object. How do i do this. I tried using the Datetime.FromFileTime(timestamp); but the output it gives is not correct. Here is the sample.

              long tm = 1217404627332;
          
              DateTime dt = DateTime.FromFileTime(tm);
              Response.Write("Time: " + dt.ToLongDateString());
          

          The output it gives is Tuesday, January 02, 1601 when it should be Wednesday, July 31, 2008 How do i do this? Any help is appreciated. Thanks in advance.

          C Offline
          C Offline
          curtisk
          wrote on last edited by
          #4

          Java time represents milliseconds since January 1, 1970, so if you account for that date instead of the January 1601 date, you should be OK.

          long tm = 1217404627332;

          DateTime dt = new DateTime(1970,1,1,0,0,0);
          dt = dt.AddMilliseconds(tm);
          MessageBox.Show("Time: " + dt.ToLongDateString());

          HTH - although I get Wednesday July 30,2008....closer! -curtisk

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups