简体   繁体   English

解析的长日期返回无效的年份参数

[英]Parsed long date returns invalid year params

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. 年份20148395无效。 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'. 该数字是从Web api获取的,并且根据开发人员的介绍,他通过Microsoft.net“ ticks”创建了这些数字。 I am trying to convert it to java date. 我正在尝试将其转换为Java日期。 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 我正在尝试从现在到很长一段时间的分钟数之间取得差异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");
    }

Seems like you have passed wrong parameter for Date class. 好像您为Date类传递了错误的参数。 java.util.Date takes milliseconds as parameter. java.util.Date以毫秒为参数。

You can use System.currentTimeMillis() to get current epoch (in milliseconds). 您可以使用System.currentTimeMillis()获取当前纪元 (以毫秒为单位)。 Although if you create an object of Date class without passing any parameter it will return current date. 尽管如果您创建Date类的对象而未传递任何参数,则它将返回当前日期。

System.currentTimeMillis() returns milliseconds since January 1, 1970 System.currentTimeMillis()返回自1970年1月1日以来的毫秒数

Eg., Date date = new Date(64060588799000L); 例如, 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. 上面的语句返回的日期为格林尼治标准时间3 12月31日23:59:59,您的示例比该时间大〜9924倍。

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. 原来我拥有的值是使用纪元,我要做的就是将1970年1月1日的纪元值减去我的长整数,然后将其除以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. 希望这对任何人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM