Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting datetime from Long  RSS feed

 
Chuck Barnes
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a value from microsoft AD that is in long type. its 'Pwdlastset' prop from a user account.

I want to convert that long value to a date time format

I can do it in .net but am having some issues in java.


In .net that long value will result in: 09/29/2010 09:45 which is correct.

Trying to do the same in java:



I get as a result: Tue Jun 03 04:55:33 EDT 4099400
Which is wrong in a couple of ways. the date is wrong and it does something with the year.

What am i missing? Im not concerned with the detail of the result, ie i dont need the dayofweek or timezone. but i do need to to return the correct date time.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not mistaken .net measures 100-nanosecond intervals that have elapsed since January 1, 0001 and Java milliseconds from January 1, 1970.
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The FILETIME structure "Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC)." So there is a quite easy way to handle this:

- get the timestamp in milliseconds for that date.
- convert the file time in 100 nanosecond intervals to milliseconds by dividing by 10,000 (dividing 100 nanoseconds intervals by 10 gives you microseconds, divide that by 1000 gives you milliseconds).
- add the two together.

You can use java.util.Calendar for the first one, but I have noticed that sometimes there is a 1 hour difference between what Windows tells me and what my Java code tells me. (Yes, I have done this myself before. That's how I can give you this information so quickly )
Other programs sometimes seem to have this issue as well though.
 
Chuck Barnes
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this:


Which gives me: 09/29/2010 09:45:10 -0400

I have to add the Z (timezone) and -0000 identifier options to the format otherwise it reports the time as 14:45:10.

Seems to be working now.

Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!