Java date object from a long

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.

Thanks
MolkoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ajay-SinghCommented:
Change it to:


File file = ...// Initialize the file.
Date date = new Date(file.lastModified());
0
MolkoAuthor Commented:
if its any help the value from

inputStream.readLong() is 129130498200000000
0
MolkoAuthor Commented:
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
};
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Mick BarryJava DeveloperCommented:
> 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?
0
VenabiliCommented:
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...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VenabiliCommented:
Just a confirmation link or two (googled after posting because wanted to verify the date on which the .NET starts counting):
http://social.msdn.microsoft.com/Forums/en-US/visualjsharpgeneral/thread/e0bbfaf0-e261-425e-b13f-b270fbc1de21
http://msdn.microsoft.com/en-us/library/system.datetime.fromfiletime.aspx

So I guess that my memory about it was correct after all. :)
0
MolkoAuthor Commented:
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 !
0
VenabiliCommented:
:) 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.
0
MolkoAuthor Commented:
Hi

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
0
VenabiliCommented:
Yeah - why MS cannot play nicely and accept the Unix time is beyond me.

Do you need any more help here? :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.