Java date object from a long

Posted on 2010-04-05
Medium Priority
Last Modified: 2013-12-29
When i  do the following

Date date = new Date(inputStream.readLong());

i get

[Mon Aug 04 10:20:00 BST 4093952]

it looks like the 'century' bit at the is not correct

Does java have an equivalent to  c#  - DateTime.FromFileTime(long val)

If not, how to i change my code to make work correctly.

Question by:Molko
LVL 23

Expert Comment

ID: 29820101
Change it to:

File file = ...// Initialize the file.
Date date = new Date(file.lastModified());

Author Comment

ID: 29824530
if its any help the value from

inputStream.readLong() is 129130498200000000

Author Comment

ID: 29826158
I think the Date should be 03/14/2010 14:17:00

The hex i am reading in is 00 56 E1 02 81 C3 CA 01

which i guess is

char hexData[8] = {
    0x00, 0x56, 0xE1, 0x02, 0x81, 0xC3, 0xCA, 0x01
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

LVL 92

Expert Comment

ID: 29847630
> inputStream.readLong() is 129130498200000000

> I think the Date should be 03/14/2010 14:17:00

what makes you think that?

that value certainly isn't, where is the value coming from?
LVL 20

Accepted Solution

Venabili earned 2000 total points
ID: 29869732
The start of MS time which .Net uses is on January 1, 1601 (as opposed to the Java one which is much later - January 1, 1970).  And they count it in 100-nanosecond intervals as opposed to the milliseconds we count in Java. Which explains the big number that you get.

So try to first convert to milliseconds ( /10000 to the value that you get) .

Your best bet would probably be to get a Java Date oject (or Calendar) from 1 Jan 1601 and to add to it the millseconds that you get from the conversion above.

See if that gets you somewhere... but that's the only way I can think of...
LVL 20

Expert Comment

ID: 29870038
Just a confirmation link or two (googled after posting because wanted to verify the date on which the .NET starts counting):

So I guess that my memory about it was correct after all. :)

Author Comment

ID: 29878689
Objects - Thanks, I know the date should be 03/14/2010 14:17:00 as the binary file I reading in had a list of dates relating to files on my PC. The files on my PC have the datetime as 03/14/2010 14:17:00.

Venabili - Thanks, that looks like a great lead, i'll read up on that now. Thanks again

Cheers guys !
LVL 20

Expert Comment

ID: 29878855
:) Have fun - I think I had done it like this at least once (or it had been the opposite direction - cannot remember now) :)

Post back if you have issues and we will try  to help.

Author Comment

ID: 29885623

I think you were very much correct. I have done some research and the NanoSeconds/Milliseconds, 1601/1970 was exactly my problem.

          long dateMs = inputStream.readLong(); //129130498200000000
          System.out.println("Date in Nanos[" + dateMs + "]");

          // Microsoft FileTime/Date Epoch is January 1, 1601
          // Java Date Epoch is January 1, 1970, so take the number and subtract java Epoch:
          dateMs = dateMs - 0x19db1ded53e8000L;
          //convert UNITS from Nanoseconds(Microsoft) to Milliseconds (java)    
          dateMs /= 10000L;
          System.out.println("Date in Ms[" + dateMs + "]");

          Date date = new Date(dateMs);
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
          String dateString = simpleDateFormat.format(date);

          System.out.println("ModifiedDate [" + dateString + "]");

And it give the correct result
LVL 20

Expert Comment

ID: 29895353
Yeah - why MS cannot play nicely and accept the Unix time is beyond me.

Do you need any more help here? :)

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A discussion about automated testing of Web Applications utilizing Selenium, along with illustrated configuration steps for the Jenkins open source tool.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question