Reputation: 6187
I have a long number that will be converted to date,
635759785190887215
Now when i parsed this number, it gives me an invalid date
from logs:
Wed Aug 02 18:48:07 PST 20148395
year 20148395
is invalid. What could have been wrong?
The number is fetched from a web api and according to the developer he created those long numbers through Microsoft.net 'ticks'. I am trying to convert it to java date. I also tried using online long to date converter and i still got the same results.
I am trying to get difference in minutes between now and the long date 635759785190887215
Here's my code:
String dateString = android.text.format.DateFormat.format("MM/dd/yyyy hh:mm:ss a",new Date(pastDateInLong)).toString();
Log.v(TAG,"PAST DATE: "+dateString);
Date lastUpdate = new Date(pastDateInLong);
Date currentDate = new Date();
Log.v(TAG,"Comparing: Now>"+currentDate.toString()+" to "+lastUpdate.toString()+ "= "+(currentDate.getTime()- lastUpdate.getTime()));
if (currentDate.getTime() - lastUpdate.getTime() >= 25*60*1000) {
Log.v(TAG,"more than 25 minutes difference");
}
else{
Log.v(TAG,"not more than 25 minutes difference");
}
Upvotes: 1
Views: 139
Reputation: 6187
Turns out that the value i have is using the Epoch, all I have to do is subtract epoch value of January 1, 1970 to my long number then divide it by 10,000.
public Date getDateFromTick(long ticks){
final long TICKS_AT_EPOCH = 621355968000000000L;
final long TICKS_PER_MILLIS = 10000;
Date date = new Date((ticks-TICKS_AT_EPOCH)/TICKS_PER_MILLIS);
Log.v(TAG, "DATE:>> (" + ticks + ") to " + date.toString());
return date;
}
Hope this helps anyone.
Upvotes: 0
Reputation: 147
Seems like you have passed wrong parameter for Date
class. java.util.Date
takes milliseconds as parameter.
You can use System.currentTimeMillis()
to get current epoch (in milliseconds). Although if you create an object of Date
class without passing any parameter it will return current date.
System.currentTimeMillis()
returns milliseconds since January 1, 1970
Eg., Date date = new Date(64060588799000L);
The above statement returns a date of 31 Dec 3999 23:59:59 GMT, your example is ~9924 times larger than that.
Upvotes: 1